How to Integrate Google reCAPTCHA v2


Topic: How to Integrate Google reCAPTCHA v2

demzl25 pro premium priority asked 6 years ago

Hi, How can i Integrate Google reCAPTCHA v2 to my contact form on my website to validate on server side using PHP? I have follow tutorial on this website: https://codeforgeek.com/2014/12/google-recaptcha-tutorial/ this is the example php code below that i have used but it fails (you can sent e-mail without marking the recaptcha). My website: https://www.regresija-hipnoza.si/kontakt

    $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
    $response = file_get_contents($url);
    $response = json_decode($response);
    if ($response->success)
        echo "Preverjanje je bilo uspešno. Your name is: $username";
    else
        echo "Preverjanje ni uspelo! Poskusite znova";
}

?> Thanks, By by


Arkadiusz Cacko staff pro premium priority commented 6 years ago

Hi @demzl25

The form on your site is not working, so I could't reproduce the issue. Could you please var_dump $response variable before checking? There is more information and maybe potential error about not successed captcha. If that will not help you to fix the problem, please send response message to me at a.cacko@mdbootstrap.com.

Regards

Arek


demzl25 pro premium priority commented 6 years ago

Hi Arkadiusz,

I want to achieve that spammers can’t sent me e-mail without ticking the Google reCAPTCHA box. My problem is with Google reCAPTCHA v2 to validate on server side using PHP so people or spammers shouldn’t be able to send the message without ticking the” I’m not a robot box” as in my contact form on my website but my Google reCAPTCHA lets people or spammers send a message without ticking the box. I need help with (PHP) to validate on server-side Google reCAPTCHA. How can I put this php code to my mail.php? So it will work properly.

if (isset($_POST['submit'])) { $username = $_POST['username']; $secretKey = "00000000000000"; $responseKey = $_POST['g-recaptcha-response']; $userIP = $_SERVER['REMOTE_ADDR'];

    $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
    $response = file_get_contents($url);
    $response = json_decode($response);
    if ($response->success)
        echo "Preverjanje je bilo uspešno. Your name is: $username";
    else
        echo "Preverjanje ni uspelo! Poskusite znova";
}

Thanks,Damjan


Arkadiusz Cacko staff pro premium priority commented 6 years ago

Hi @demzl25

Ok, I thought the problem is that captcha not response correctly. I can not see where You use function mail(), but You have to prevent this action if captcha failed. Here is small example how it works:

 if ($response->success) {
    echo "Preverjanje je bilo uspešno. Your name is: $username";
    mail(...) // <-- Captcha sucessed so mail will be send
} else {
    echo "Preverjanje ni uspelo! Poskusite znova";
    // In this case captcha failed so we prevent to start mail function
}

Please insert min. 20 characters.

FREE CONSULTATION

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

Status

Open

Specification of the issue
  • User: Pro
  • Premium support: Yes
  • Technology: MDB jQuery
  • MDB Version: 4.5.5
  • Device: destop
  • Browser: chrome
  • OS: window 10
  • Provided sample code: No
  • Provided link: Yes
Tags
php