PHP form + Google reCAPTCHA

淺唱寂寞╮ 提交于 2019-11-30 04:02:54
Alagunto

Check out this link: https://developers.google.com/recaptcha/docs/verify

In a few words, you should make request to

https://www.google.com/recaptcha/api/siteverify?secret=YOUR_SECRET&response=RESPONSE_CAME_FROM_YOUR_FORM&remoteip=USER_IP_ADDRESS  

Where YOUR_SECRET is secret key you received on ReCAPTCHA site, USER_IP_ADDRESS can be received through $_SERVER array and RESPONSE_CAME_FROM_YOUR_FORM is a string sent with your form. It is stored in $_POST['g-recaptcha-response'].

You can do it via file_get_contents($url) like

$data = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR_SECRET&response=RESPONSE_CAME_FROM_YOUR_FORM&remoteip=USER_IP_ADDRESS");

In $data you will receive JSON object containing success field, which you are looking for. If success is false, then it is not a human and you should exit(). I suggest you checking this in the beginning of your program.

Update:

Decoding of JSON object looks like:

$data = json_decode($data); // This will decode JSON to object
if(!$data->success) 
    exit();

Update:

Sometimes, file_get_contents($url) won't be able to set up secured https connection. Instead you can use open_https_url($url) Make your code look like:

<?php
    $your_secret = "<secret_key_you_received_from_recaptcha_site>";
    $client_captcha_response = $_POST['g-recaptcha-response'];
    $user_ip = $_SERVER['REMOTE_ADDR'];

    $captcha_verify = open_https_url("https://www.google.com/recaptcha/api/siteverify?secret=$your_secret&response=$client_captcha_response&remoteip=$user_ip");
    $captcha_verify_decoded = json_decode($captcha_verify);
    if(!$captcha_verify_decoded->success)
      die('DIRTY ROBOT');

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $human = $_POST['human'];
    $from = 'From: My Website';
    $to = 'myemail@gmail.com';
    $subject = 'Request Form';

    $body = "Name: $name \n E-Mail: $email \nMessage:\n$message";

    if ($_POST['submit']) {
        if ($email != '') {
            if ($human == '4') {                 
                if (mail ($to, $subject, $body, $from)) { 
                    echo '<p>You have successfully submitted your information to PS4RS. Subscribers to our mailing list will begin to periodically receive updates.</p>';
                } else { 
                    echo '<p>Something went wrong, go back and try again!</p><p><input type="button" value="Go Back" onclick="history.back(-1)" class="goback" /></p>'; 
                } 
            } else if ($_POST['submit'] && $human != '4') {
                echo '<p>You answered the anti-spam question incorrectly!</p><p><input type="button" value="Go Back" onclick="history.back(-1)" class="goback" /></p>';
            }
        } else {
            echo '<p>You need to fill in all required fields!!</p><p><input type="button" value="Go Back" onclick="history.back(-1)" class="goback" /></p>';
        }
    }
?>
alex351

Maybe the above answer is a bit outdated, since Google is now using reCaptcha nocaptcha. I found a simpler and more complete answer here to use with your separate php email file.

The solution has a simple email form with name and email and a separate php file to submit the form. You should be able to go on from there and tweak your form accordingly. The solution worked for me.

https://stackoverflow.com/a/27439796/3934886

and link to tutorial:

http://codeforgeek.com/2014/12/google-recaptcha-tutorial/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!