on air

Adding CAPTCHA To PHP Form.

back home » Scripting Tutorials » PHP » Adding CAPTCHA To PHP Form.


CAPTCHA is a security program usually attached to Web Forms. So only human can fill out a form completely, executing an action. It's the immigration part of a Web Form that requests, users re-typing its generated distorted words into a field for verification. And if words entered correctly before users' details will be allowed passing through, or a script executed. This point is where non human programs can't fulfill, so bots will have to stay behind.

There are different types of CAPTCHAs online. However, we'll be rolling with the reCAPTCHA.

Are You Ready To Implement A reCAPTCHA On Your Website?

If you're ready, then I'll assume you've completed creating your Web Form and the Web Form Processing file. And all you need now is attaching a CAPTCHA script to them.

If you are yet to, then start learning the tutorial about how to create, and process Web Forms using PHP before starting this CHAPTCHA tutorial. If done, then follow-up and learn how to implement a CAPTCHA onto your Website right ahead.

Getting Prepared Attaching reCAPTCHA On PHP Form.

There are many ways implementing a CHAPTCHA onto your Website. However, its your decision choosing the method you'll want to use, applying a CAPTCHA onto your Website.

In this tutorial, we are going to learn how to attach reCAPTCHA onto a PHP Form using reCAPTCHA plug-in.

First of all, you'll need to sign-up for your reCAPTCA API keys [Get Private and Public keys].

And then download the reCAPTCHA PHP Library. This is a zip-file containing three PHP files and two text files, the only file you'll need to copy into your Web Form folder from here, is the recaptchalib.php file. The rest are not file that will affect the functionality of reCAPTCHA so you may ignore them. However, reading them may be useful for other purposes.


Zip-File Contents:

  • Readme.txt
  • License.txt
  • example-captcha.php
  • example-mailhide.php
  • recaptchalib.php [required]

As said earlier, you may review the files above for futher information, however, only the recaptchalib.php file is required.

Displaying The reCAPTCHA Widget On Your Website [User Side].

Now, you'll copy the code below as is and paste it inside your <form> element, where you'll want the reCAPTCHA image to display. Usually, it's placed on the line before the <input> element holding your Submit button.

When done, replace the part of the code reading "your_public_key" with the public key you got from the reCAPTCHA sign-up page. [Replace not your public key with the private key].


Displaying the reCAPTCHA image on your website doesn't make it work directly. You should remember, this is the client side scripting of the whole process.

We'll also need to configure the server side scripting which will contain your private key. Plus, this is a step by step tutorial and needs to be followed accordingly.

Study and re-type or copy and paste the code below inside of your <form> element.

require_once ('recaptchalib.php');
$publickey = "your_public_key"; 
echo recaptcha_get_html ($publickey);

After adding the above code into your <form> element, and replacing the part that says, "your_public_key" with the right public key, your HTML code should end up looking somehow like this. See script below.

<!--Document Content-HTML-->
<form name="userdata" method="post" action="verify.php">
require_once ('recaptchalib.php');
$publickey = "5JbAP1cYVVMMMMsx6S3agLKJnK-2LZMaLhaHqKfD"; 
echo recaptcha_get_html ($publickey);
<input type="submit" />
<!--Additional Document Content-HTML-->

FastLane Tip #2:

The main purpose of creating a CAPTCHA is, building a frontier between your Web Form and users. And this means, users' actions needs to be verified making sure they aren't bots before allowing them.

The <form action="verify.php">

If you've noticed, the "action" attribute inside the <form> element above has a value that reads [verify.php].

This is the destination file that will contain the verification script the form's content will be submitted to. [Advance users may place file as desired and path to it].

In our case, "verify.php" must exist in the same folder as the Web Form file, and this's usually the same PHP file that was created in the create, and process Web Form using PHP tutorial, used in processing the Web Form [userdata.php].

Now, all you'll want to do is, including a server sided script at the top of the " verify.php" script. Learn how to do that in the next step.

The reCHAPTCHA Verification Script [Server Side].

If you've learnt the tutorial on how to create, and process Web Form using PHP, you'd have noticed that we created two files, one for the Web Form [uaserdata.php], and one for processing the Web Form [verify.php].

Now you'll copy and paste the code below right at the top of the script inside the " verify.php" file, and replace the part of the code that reads, " your_private_key" with the private key you got from the reCAPCHA sign-up page. Proceed as we did above.

[Be extra careful and not Replacing your private key with the public key].

Study and re-type or copy and paste the code below, to the top of "verify.php" script.

require_once ('recaptchalib.php');
$privatekey = "your_private_key"; 
$resp = recaptcha_check_answer ($privatekey,
		$_POST ["recaptcha_challenge_field"],
		$_POST ["recaptcha_response_field"]);

if (!$resp->is_valid) {
die ("The reCHAPTCHA entered was incorrect. Retry entering it correctly.".
	"(reCAPTCHA	 SAID: " . $resp->error . ")");
} else {
	/*you can, on the other way round copy and paste the verify.php script here,
 	saving this to replace the old.*/

The reCAPTCHA Standard Themes.

The reCAPTHCA widget comes always with its standard themes, and they are named as follows:

Recaptcha Theme
  • 'red' [default theme]
  • 'white'
  • 'blackglass'
  • 'clean'

Now, to apply a different theme than the default, you'll need to copy and paste the code below into the <body> of your HTML document. And replace the part of the code that says 'theme_name' with one of the standard theme names above.

The whole code must be placed before the <form> element holding the reCAPTCHA script.

Placing the code behind the main script where reCAPTCHA was first invoked will not work.

Study and re-type or copy and paste the code below, before your <form> element.

<script type="text/javascript">
var RecaptchaOptions ={
theme : 'theme_name'
FastLane Tip#3

Please note that before you can preview, your files must be uploaded onto a web server.

How To Add reCaptcha.

Hope you're able to add it, if not try again. Good Luck.


Man will give you worldly powers, and make you feel on top of all, note this is temporally and could be redrawn at anytime sending you back to the ground floor of life. However, the Lord Almighty, the one and the only rightful ruler of the Universe, blesses everlastingly.


Knowledge is Power, but Power is not Knowledge. Wisdom is the truthful Power that identifies the Knowledge within the man. And only, I repeat 3x. Only the almighty God has the power to bless the man with his Wisdom forever.

To achieve the Lord's blessings, cease not praying daily, asking for his Wisdom, and you'll be blessed abundantly...

Advertise your business here

Advertising your business means a lot to us. For less than 20€, you can place your business ad here and start receiving traffic.

Place your ad here


Spyera Phone

Spyera Phone version is software that you install on a smart phone to monitor everything happening on the phone. After installation, SPYERA secretly records events (sms, call history, phone book, location, emails, What's App messages, IM, Facebook Chat, Skype, See photos taken and many more...) that happen on the phone and delivers these information to a web account, where you can view these reports 24/7 from any Internet enabled computer or mobile phone. SPYERA also allows you to listen to the surroundings of the target mobile , listen to the phone conversation and to know the location of the device.

Please, Kindly Donate What You Can, Enabling Us Pay Hosting And Domain Costing, And Do More.

Please Donate