Use reCAPTCHA in CakePHP App

Published on : January 2, 2013

Author:

Category: CakePHP


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 your 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. Fourtunately google hosted https version as well so we are going to use http://www.google.com/recaptcha/api/js/recaptcha_ajax.js. Add this to your layout.


echo $this->Html->script('http://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 controller. Because the captcha text field 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.');
    }
}


Leave a Reply

Your email address will not be published. Required fields are marked *