Implement reCaptcha with PHP Form

Web robot is getting more and more advance everyday. They are able to index through the website which implemented online contact us form. The next thing you know if getting more spam into your mailbox from the unprotected contact us form.

reCaptcha came into response to stop those spam. reCaptcha is another great service offer by Google (for free now). Helping your and people in the web’s world to fight spam. There is new version of reCaptcha, initially reCaptcha1 and now latest is reCaptcha2.

There is no sample code showing how to implement reCaptcha2 with PHP form. We have written and short and sweet sample giving you a head up. Follow the steps below to find out how to implement reCaptcha and PHP Code, we use contact form as an example here.

1. Register with reCaptcha

It’s fairly simple, just head over to Google’s reCaptcha site. Click on “Get reCaptcha” button on top right corner.

2. Add a Website Domain

You are required to add the domain name which will implement reCaptcha, i.e, and do not forget to include subdomain as well, i.e and, both also will reach out to our page for an example. Hit on Register to proceed.


3. The Keys

By now you should have a Secret Key and a Site Key. Site Key will be use on the form, to generate the reCaptcha image, Secret Key will be used to perform cross check with reCaptcha is this “human” verified.

Both keys will be used for the sample up next.

4. PHP Contact Form and reCaptcha Codes

Below is the sample PHP Contact form, which has reCaptcha verification implemented. The code is available on Github as well.

A brief explanation

  • Line 1 – 49 handling submission of the form.
  • Line 10 – Required your Secret Key to be inserted
  • Line 57 insertion of reCaptcha library file directly from Google.
  • Line 102 – Required your Site Key to be inserted
  • Line 12 – After form submission, it will do a server check with reCaptcha is the session being authorized. If the return of “success” value is “1”, that’s mean it’s successful.

Running a test of the Contact Us submission on the form, without clicking on “I am not a robot”, the submission of the form will fail.

If you see error message from debug output $reSubmission variable (do print_r($reSubmission) for debug); you can refer to reCaptcha documentation for more information.

Above is the sample of by using Contact Us form, it will be useful if you are doing other kind of form which required user data entry submission, protect your form and unwanted data from the internet with reCaptcha 😉