Topic: How to implement Anti-spam Math capctha and Honey pot to contact form

demzl25 premium asked 4 months ago


Expected behavior

How to implement PHP Anti-spam Math capctha and Honey pot from this examples?

Math capctha:

https://webdesign.tutsplus.com/tutorials/simple-math-captcha-for-your-forms-with-php-and-javascript--cms-41653

Honeypot:

https://gist.github.com/andrewlimaza/958826feac907114a57462bfc8d535ff

Actual behavior

PHP is not working in html and im not sure how to implement PHP code from this examples to my mail.php and connnect with bottons and form to get it working.

My mail.php:

include_once("./mail_settings.php");

$name = (isset($_POST['name'])) ? $_POST['name'] : '';

$email = (isset($_POST['email'])) ? $_POST['email'] : '';

$message = (isset($_POST['message'])) ? $_POST['message'] : '';

$phone = (isset($_POST['phone'])) ? $_POST['phone'] : '';

if ($name === '') {

die("Prosimo, izpolnite zahtevano polje."); }

if ($email === '') {

die("Izpolnite zahtevano polje!.");

} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

die("Email je neveljaven.");

}

if ($phone === '') {

die("Prosimo, izpolnite zahtevano polje.");

}

if ($message === '') {

die("Prosimo, izpolnite zahtevano polje.");

}

$content="From: $name \nEmail: $email \nMessage: $message \nPhone: $phone";

$recipient = "info@regresija-hipnoza.si";

$mailheader = "Sporocilo: $email \r\n";

$email_subject ='Regresija-hipnoza';

$mail->Subject = $email_subject;

$mail->Body = $content;

$mail->AltBody = $sporocilo;

$mail->SetFrom($email, $name);

$mail->AddCC($recipient, "regresija hipnoza");

$mail->send();

if (headers_sent())

echo "window.top.location='https: //www.regresija->hipnoza.si/zahvalna-stran.html';";

else

header("location: https://www.regresija-hipnoza.si/zahvalna-stran.html", true, 301 );

?>

Resources (screenshots, code snippets etc.)

Link to my contact form snippet that i added PHP code:

https://mdbootstrap.com/snippets/standard/demzl25/3912833#html-tab-view


Grzegorz Bujański staff answered 4 months ago


Heney pot is just hidden input and condition to check if input has not been fulfilled. So you just need to add PHP code to mail.php and HTML code to your HTML file.

It is a bit different with captcha. This requires the execution of PHP code in the same file. But it is enough to convert this code into JS:

HTML

<span id="firstNumber"></span>
<span class="ms-1">+</span>
<span id="secondNumber"></span>

JS

const number1 = document.querySelector('#firstNumber');
const number2 = document.querySelector('#secondNumber');

const min = Math.ceil(1);
const max = Math.floor(300);
const num1 = Math.floor(Math.random() * (max - min)) + min;
const num2 = Math.floor(Math.random() * (max - min)) + min;
const sum = num1 + num2;

number1.innerText = num1;
number2.innerText = num2;

Now you just have to check to submit that the input value is the same as the variable sum

Of course the second solution is to create a form in a php file instead of using an HTML file


demzl25 premium commented 4 months ago

Great thanks, that is helpful.


Grzegorz Bujański staff answered 4 months ago


From what I understand are you trying to use PHP in an HTML file? You should do this in PHP file.


demzl25 premium commented 4 months ago

Yes I would like to add PHP code of Math capctha and Honey pot in mail.php and connect PHP with js or Ajax or somehow. Is this possible or there is some better way?



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • User: Premium
  • Premium support: Yes
  • Technology: MDB Standard
  • MDB Version: MDB5 4.2.0
  • Device: Pc
  • Browser: Chrome 97.0.4692.99
  • OS: Windows 10
  • Provided sample code: No
  • Provided link: Yes