PHP form + Google reCAPTCHA

It is strange that the Google documentation for recaptcha is not as useful as I thought it would be. I was asked to accept an existing existing form (which receives spam several times a day) and update it using Google new recaptcha. There are many textbooks for old captcha, but not so much for new. I just want a simple form to capture the name, email address, message, and then replace my current β€œanti-bot field” with recaptcha (I used a field that basically asked you what 2 + 2 is, and if you entered something, but 4, he did not send). If the required fields are valid and the validation is valid, I want her to send me an email with the contents of the form fields.

I went through simple steps:

  • registered my site to get keys

  • added this snippet inside my tag:

    <script src='https://www.google.com/recaptcha/api.js'></script> 
  • added this snippet at the end of my form:

     <div class="g-recaptcha" data-sitekey="#MYKEY#"></div> 

At this point, recaptcha is displayed just fine. But the server side part is a bit confusing.

This is my updated recaptcha contact form:

 <form method="post" action="contact-post.php"> <label>Your Name (required):</label> <input name="name" type="text" placeholder="Enter your name here"> <label>Email Address (required):</label> <input name="email" type="email" placeholder="Enter your email address here"> <label>Your Message (required):</label> <textarea name="message" placeholder="Write your message here"></textarea> <div style="margin-top:20px;" class="g-recaptcha" data-sitekey="#MYKEY#"></div> <input id="submit" name="submit" type="submit" value="Submit Form"> </form> 

And here is my current POST page (I'm not sure where to add the recaptcha code):

 <?php $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>'; } } ?> 

Any help is appreciated. I feel that these can be quite common people with people trying to incorporate them into their current work forms.

+6
source share
2 answers

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

In a few words you should make a request

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

If YOUR_SECRET is the secret key that you received on the ReCAPTCHA website, USER_IP_ADDRESS can be obtained through the $_SERVER , and RESPONSE_CAME_FROM_YOUR_FORM is the line submitted with your form. It is stored in $_POST['g-recaptcha-response'] .

You can do this via file_get_contents($url) , e.g.

 $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 get a JSON object containing the success field you are looking for. If success is incorrect, then this is not a person, and you must exit() . I suggest you check this out at the beginning of your program.

Update

Decoding a JSON object looks like this:

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

Update

Sometimes file_get_contents($url) will not be able to configure a secure https connection. Instead, you can use open_https_url($url) Make your code look like this:

 <?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>'; } } ?> 
+5
source

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

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

fooobar.com/questions/99568 / ...

and a link to the tutorial:

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

+1
source

Source: https://habr.com/ru/post/980237/


All Articles