Sarathlal N

Using Google reCAPTCHA with PHP

The reCAPTCHA is a Google captcha service & it is widely used on website & blogs to prevent spam comments & automated extraction of data from websites.

Today we are going to use reCAPTCHA service in our PHP web application to familiarize with reCAPTCHA & its usage.

Before implementing reCAPTCHA, just know that it was started as a college project in Carnegie Mellon University’s Pittsburgh campus, and acquired by Google in September 2009.

The user submitted text are used to digitize books and building machine learning datasets. More than 200 million ReCaptcha’s are entered every day and that relates to about 150,000 man hours of work!

Anyway, if we want to use reCAPTCHA, first we want to sign up for reCAPTCHA service. We can use our Google account for this purpose.

After sign up, just add our domain name. Then the reCAPTCHA service will generate some special keys for us. The reCAPTCHA only works on our specified domain & its sub domains only.

By default, all keys work on localhost (or So for testing purpose, if we don’t have a domain name yet, we can use a demo domain name.

Before any further steps, note down our special keys for our domain. They are known as public & private keys. We want to use them in next steps.

To simply use reCAPTCHA with PHP, we want to download reCAPTCHA PHP library from here. We only need a single file (recaptchalib.php) from that collection.

Client Side – How to make the CAPTCHA image show up

Normally we use captchas in forms & user submissions. So to display reCAPTCHA in a form, we want to insert a small snippet of code inside the <form> element where the reCAPTCHA widget will be placed.

$publickey = "our_public_key"; // we got this from the signup page
echo recaptcha_get_html($publickey);

In this code snippet, first line include our downloaded PHP recaptchalib.php. The second line is about our public key. Just copy paste our public key at there.

Now our HTML form in PHP page look like below one.

<form method="post" action="verify.php">
	  $publickey = "your_public_key"; // you got this from the signup page
	  echo recaptcha_get_html($publickey);
	<input type="submit" />

Server Side – How to test if the user entered the right answer

In our form, the target to submit data is verify.php. So we add another snippet of code at the top of our form target file.

$privatekey = "our_private_key";
$resp = recaptcha_check_answer ($privatekey,

if (!$resp->is_valid) {
  // What happens when the CAPTCHA was entered incorrectly
  echo "You are not human!";
} else {
  // our code here to handle a successful verification
  echo "You are verified as human!";

For simplicity, I just echo a message according to verification. We want to replace that messages with our actual code to be happened after verification.

Try this on our localhost & surely it will help us in our upcoming projects.

Recent Posts

  1. Solve "ValueError invalid literal for int() with base 10" - Python
  2. Handling dynamic subdomain with Flask - Python
  3. A minimal example about WordPress object cache
  4. Select random element from a list - Python
  5. Write our first Selenium program with Python 3 & Firefox

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.