Free Android CakePHP GMaps Articles by Bali Web Design

October 13, 2010

Captcha component for cakephp using captcha.net

Filed under: cakephp,php — admin @ 8:32 pm

Captcha is a type of challenge-response to ensure that the user is really a person that the response is not generated by computer. We usually find captcha on any form input on a website such us contact us, feedback, registration and etc. The captcha is used to protect our site from spammer.

The popular captcha is recaptcha from captcha.net website. It is free for use. Their interface or design also really nice to put on your website. You can check their sample in http://www.captcha.net/. They support both image captcha and audio chaptca.

Since i work with cakephp framework, i need to integrate this captcha into my application. I would like to share my captcha component for you. It is a simple code and easy to integrate. Here are the step

  1. Download the library here and you can read their document for the manual in this page.
  2. Extract recaptcha-php-1.11.zip ( or latest version ) into yourcakephpfolder/app/vendors/recaptcha-php. So the recaptchalib.php will have path on yourcakephpfolder/app/vendors/recaptcha-php/recaptchalib.php
  3. Get public key and private key for your domain here. You will need an google account for login
  4. And then create new component CaptchaComponent on your cakephp. This is the code for file yourcakephpfolder/app/controllers/components/captcha.php
    <?php
    App::import('Vendor', 'recaptcha-php', array('file'=>'recaptcha-php'.DS.'recaptchalib.php'));

    class CaptchaComponent extends Object {
    var $controller;
    var $public_key = 'your_recaptcha_public_key';
    var $private_key = 'your_recaptcha_private_key';

    function startup( &$controller ) {
    $this->controller = &$controller;
    }

    function create_image_captcha() {
    return recaptcha_get_html($this->public_key);
    }

    function check_captcha() {
    $resp = recaptcha_check_answer($this->private_key, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
    return $resp->is_valid;
    }
    }
    ?>

  5. Include CaptchaComponent in any controllers that you want to add a captcha
    var $components = array('Captcha');
  6. And then inside the action function, create new captcha
    $this->set('captcha_code', $this->Captcha->create_image_captcha());
  7. You can use $captcha_code inside your view, place and echo this variable on appropriate place inside the form.
  8. To check user captcha input, you can check using this $this->Captcha->check_captcha(), it will return true if user input correct captcha, and false if incorrect captcha

That is a simple Captcha component for cakephp using popular captcha.net. I hope this help you againts spammer on your website. If you get any difficulty of using this component, just write a comment below, i will try to answer your question and fix the problems.

My Demo for Captcha Component


1 Comment »

  1. Hi,

    How are you today?. Hope your fine. I have tried that captcha script. When I submit the form I got this error message ‘Could not open socket’. How can fix this error.

    Comment by Anand — February 11, 2012 @ 1:51 am

RSS feed for comments on this post. TrackBack URL

Leave a comment