Have you seen website registration page lately? See that image where there are texts, words, alphanumeric or phrase that you need to type? That is what we called CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart). Before, if you want to incorporate captcha into your page, you have to make some functions in php to convert your string into an image. And you have to enable image functions of PHP to generate the image. Not this time! No more hassle. Just use reCAPTCHA.
reCAPTCHA is a free, secure and accessible CAPTCHA implementation. Today let us talk on how to incorporate reCAPTCHA to your registration page. I assume that you already know how to make a registration form and that the current task that you are going to do right now is just to integrate reCAPTCHA.
To start with, you have to create a reCAPTCHA account. Follow this link. Once you have created your account, type the domain name. You can choose the “Enable this key on all domains (global key)” if you will be using the key for several domain/websites.
Click the CREATE KEY button. You have to remember your public and private key because you will be using that one in your code. Before we will go back to your code, download this recaptcha library.
Now back to your code. Supposing you have
<form name=”form1? method=”post” action=”testformsave.php”>
First name: <input type=”text” name=”firstname” id=”firstname”>
Last name: <input type=”text” name=”lastname” id=”lastname” />
<input type=”submit” name=”cmbsubmit” id=”cmbsubmit” value=”Submit”>
</form>
Insert this code after the Last Name line
<?php
require_once(’recaptchalib.php’);
$publickey = “”;
echo recaptcha_get_html($publickey);
?>
See to it that the library is on the same path as your registration page. The $publickey should contain the key that you have from reCAPTCHA.
Once that is done, go to your page that handles the form submitted. Let us say you have all ready the script that will save registrant’s data to your database. You can see my other tutorial here. All you have to do is to inject on the top the
<?php
require_once(’recaptchalib.php’);
$privatekey = ” “;
$resp = recaptcha_check_answer ($privatekey,$_SERVER["REMOTE_ADDR"],$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid)
{
die (”The reCAPTCHA wasn’t entered correctly. Go back and try it again.” . “(reCAPTCHA said: ” . $resp->error . “)”);
}
else
{
put your code here.
}
?>
The $privatekey variable should contain the key given to you by reCAPTCHA. Thats it! You have made your registration page as spam-free as possible.
Happy coding
About the Author:
Felix Gomez Jr is a web developer and a blogger. Visit his blogs at www.shoutbux.com
Article Source: ArticlesBase.com - Using Recaptcha to your registration form
[affmage source="amazon" results="5"]php or die error[/affmage]
