How to add recaptcha in Zend Form


Its an simple way to add the recaptcha service in your web form.

Zend framework reduce your development time by providing the service through its Zend_Service_ReCaptcha.

Step-1: You need to signup an account for your domain name or project. Why  you should do this?

Because for using the recatcha service you need two key public key and private key.

And it will be generated when you will signup in the signup link.

Then you will find a signup button.Click the button

When you will click it and login via your gmail account then you will find the page

https://www.google.com/recaptcha/admin/create

Enter your project name or domain name you will get the two key one will be public key and other is the Private key.

Actually to get the repcaptcha service you need this two key.
Step-2:
Now the time for coding. Dont get monotonous. Because it is too simple that you never seen it!!! Really! Yes.

Create an instance of the reCAPTCHA service

$recaptcha = new Zend_Service_ReCaptcha($pubKey, $privKey);

Put your two key which you got from recatcha service. Like the following line.

$recaptcha = new Zend_Service_ReCaptcha(‘6LcopMUSAAAAALEItFjsw_cpU9C6q8CArtTd8Mwy ‘,’6LcopMUSAAAAAKOLIiYrK6wEJILuHPtNuIa841ep’);

And in the form script just write the code:

echo $recaptcha->getHTML();

And if you view the form from your browser you will get the recaptcha like the image: May be you can see that you have to type two words. And so developer need to verify the match of the value And the code for this.

$result = $recaptcha->verify(
$_POST[‘recaptcha_challenge_field’],
$_POST[‘recaptcha_response_field’]
);

And you can check the result is valid or not. Then code is:

if ($result->isValid()) {

echo”Ok you are not spam”;

}

And this is the setting and coding pattern for Zend recaptcha. If you have any question regarding this comment option is open for you.

Cheers!!!


Leave a Reply