Use reCAPTCHA in CakePHP App

Update:According to http://blog.jambura.com/2013/01/02/use-recaptcha-in-cakephp-app/?fb_comment_id=fbc_576497325700235_7862505_715928588423774 you have to use following code if you are using CakePHP 2.x.x

$this->request->data('recaptcha_challenge_field'),
$this->request->data('recaptcha_response_field'));

reCAPTCHA is a free ANTI-BOT service from google. This service is pretty good and very easy to implement. I have implemented this on my recent CakePHP app. First you should do is sign up and register you domain for private and public key. Then download reCAPTCHA PHP library and put recaptchalib.php file on recaptchalib folder on verdors folder like vendors -> recaptchalib -> recaptchalib.php .

Now put those key in bootstrap.php

Configure::write('recatpch_settings', array(
	'public_key'=>'public key',
    'private_key'=>'private key'
));

I am going to use AJAX API to render the reCAPTCHA. So we need to add recaptcha_ajax.js to our layout. Fourtunalty google hosted https version as well so we are going to use https://www.google.com/recaptcha/api/js/recaptcha_ajax.js. Add this to your layout.

echo $this->Html->script('https://www.google.com/recaptcha/api/js/recaptcha_ajax.js'); 

I like to initialize the Recaptcha when document is ready. recaptcha_div is the id of the div which will be replaced by Recaptcha.

$(function(){
    Recaptcha.create("<?php echo Configure::read("recatpch_settings.public_key")?>"", 'recaptcha_div', {
    theme: "red",
    callback: Recaptcha.focus_response_field});
});

Now we are going to build simple from to enter name and email

<?php echo $this->Form->create('ContactMsg', array('type'=>"file",'inputDefaults' => array('label' => false,'div' => false)));?>
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td width="114" >Full Name :*</td>
        <td width="284" ><?php echo $this->Form->input('name');?></td>
    </tr>
    <tr>
        <td width="114" >Email :*</td>
        <td width="284" ><?php echo $this->Form->input('email');?></td>
    </tr>
    <tr valign="top">
        <td width="114" >&nbsp;</td>
        <td width="284">
            <div id="recaptcha_div"></div>
        </td>
    </tr>
    <tr valign="top">
        <td width="114" >&nbsp;</td>
        <td width="284">
            <?php echo $this->Form->submit('Submit')?>
        </td>
    </tr>
</table>
<?php echo $this->Form->end();?>

Now we need to varify captcha code in our cintroller. Because the captcha texfield is not according to CakePHP we have to use $this->params[‘form’].

App::import('Vendor', 'recaptchalib', array('file' => 'recaptchalib/recaptchalib.php'));			
$resp = recaptcha_check_answer (Configure::read("recatpch_settings.private_key"),
                    $_SERVER["REMOTE_ADDR"],
                    $this->params['form']["recaptcha_challenge_field"],
                    $this->params['form']["recaptcha_response_field"]);
if (!$resp->is_valid) {
    $this->Session->setFlash('The reCAPTCHA wasn\'t entered correctly. Please, try again.');
} else {
    $this->ContactMsg->create();
    if ($this->ContactMsg->save($this->data)) {
    	$this->Session->setFlash('We recived you request.', true));
    } else {
        $this->Session->setFlash('We could not save your request. Please, try again.');
    }
}

About Zakir Hyder

This entry was written by .

02. January 2013 by Zakir Hyder
Categories: CakePHP, Library, PHP | Tags: , , | Comments

Comments

  1. […] Use reCAPTCHA in CakePHP App January 2, 2013 5:28 PM reCAPTCHA is a free ANTI-BOT service from google. This service is pretty good and very easy to implement. I have implemented this on my recent CakePHP app. First you should do is sign up and register you domain for private … Continue reading → […]

  2. […] Categories: CakePHP, Library, PHP Tags: cakephp, reCAPTCHA, Vendor 1 comment […]

  3. […] following this tutorial on how to add recaptcha on my cakephp2.2.5 project. Unfortunately everytime I hit the […]