Difference between #2 and #1 of Using recaptchlib in your projects

unchanged
Title
Using recaptchlib in your projects
unchanged
Category
How-tos
unchanged
Tags
interface, captcha, libraries
changed
Content
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](http://www.recaptcha.net "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](http://code.google.com/p/recaptcha/downloads/list?q=label:phplib-Latest
"recaptcha php")

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**### The Display  
At the top part of our view we include the recaptcha library and set our public
key like this:

~~~
[php]

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:

~~~
[php]
 echo recaptcha_get_html($publickey); ?>
~~~

**The Confirmation**### The Confirmation  
In our last step, we need to code the following in the controller action that is
going to receive the form parameters:



~~~
[php]
.... 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.