You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
It is good when someone writes extensions for us but trust, sometimes we forget about other approaches that, if we know the library, we shouldn't forget.
This small tutorial is to explain how to make use of the well known library recaptcha.
Please note that I don't have any problems with Yii's captcha feature, nevertheless we should admit that the design of recaptcha is much more appealing, isn't it?
Installation ¶
1) First of all we need to download the correspondent recaptcha php libraries
2) Unzip and move to your applications/protected/extensions folder
3) Now, you need to go to www.recaptcha.net and add your domain to get your public and private keys - dont worry for the sake of this tutorial I already have the keys for the domain 'localhost', so we can test it in our computers. Here they are:
a) Public Key: 6LfgNLoSAAAAAGMmhFTEpmRKN82P9z98MqjmI_s3
b) Private Key: 6LfgNLoSAAAAAGBVyqeKK9qH_wG5HIGIDd2KotLB
Ok, we are set... lets use the library
How to use ¶
There are two parts we have to take into account: 1) the display -in our view 2) and the confirmation -in our controller
The Display At the top part of our view we include the recaptcha library and set our public key like this:
Yii::import('ext.recaptchalib',true);
$publickey = "6LfgNLoSAAAAAGMmhFTEpmRKN82P9z98MqjmI_s3";
The public key is important as afterwards in the view (wherever we are going to render the captcha in our form) we are going to tell the library to render the captcha with the key as a parameter:
echo recaptcha_get_html($publickey); ?>
The Confirmation In our last step, we need to code the following in the controller action that is going to receive the form parameters:
.... actionWhatever(){
// I check against a value instead of isPostRequest
// but this is something that is up to you
if(Yii::app()->request->getParam( 'submit' )){
// importing the library -I am forcing include
Yii::import ( 'ext.recaptchalib', true );
// remember our private key?
$privatekey = '6LfgNLoSAAAAAGBVyqeKK9qH_wG5HIGIDd2KotLB';
// here comes the good part
// lets check if the code submitted is the correct one
$resp = recaptcha_check_answer(
$privatekey, // our private key
$_SERVER ["REMOTE_ADDR"], // here it will check our domain
// the challenge of the recaptcha library posted
Yii::app()->request->getParam( "recaptcha_challenge_field" ),
// the code inserted by the user in the captcha field
Yii::app()->request->getParam( "recaptcha_response_field" )
);
// recaptcha_check_answer returns a ReCaptchaResponse object
// which has two properties: is_valid and error.
// I dont think I need to explain that
if($resp->is_valid)
{
// ... success
}
else
{
// .. failure
}
And that's it, your very own recaptcha implementation.
recptchilb
where i put actionWhatever(), can you help me ? could you put one example, please?
reCAPTCHA's plugin available
There's reCAPTCHA's Yii plugin available for all those, that want to implement ready solution, out-of-the-box, instead of coding it itself, from scratch, as it is described in this article.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.