EllisLab text mark
Advanced Search
1 of 2
1
   
reCAPTCHA helper and validation function (easy-to-use captcha)
Posted: 19 October 2008 03:36 PM   [ Ignore ]
Joined: 2008-07-13
45 posts

This is a widely used captcha implementation. It’s used at big sites like Facebook and CraigsList.

Steps to Use

1. Sign up for an access key at http://recaptcha.net/

2. Get recaptchalib.php from the zip archive located at http://code.google.com/p/recaptcha/downloads/list?q=label:phplib-Latest.

3. Rename recaptchalib.php to recaptcha_helper.php and put it in your helpers folder.

4. Add the following function to recaptcha_helper.php:

function recaptcha()
{
    $CI 
=& get_instance();
    
$CI->config->load('recaptcha');
    
$public_key $CI->config->item('recaptcha_public_key');
    
$message = isset($CI->validation->recaptcha_error
             ? 
$CI->validation->recaptcha_error '';
    return 
recaptcha_get_html($public_key$message);

4. Add the following function to your MY_Validation class as seen below in the libraries folder. (If you don’t already have one a MY_Validation class, make one.)

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class 
MY_Validation extends CI_Validation
{
    
    
function MY_Validation()
    
{
        parent
::CI_Validation();
    
}
    
    
function recaptcha_matches()
    
{
        $CI 
=& get_instance();
        
$CI->config->load('recaptcha');
        
$public_key $CI->config->item('recaptcha_public_key');
        
$private_key $CI->config->item('recaptcha_private_key');
        
$response_field $CI->input->post('recaptcha_response_field');
        
$challenge_field $CI->input->post('recaptcha_challenge_field');
        
$response recaptcha_check_answer($private_key,
                                           
$_SERVER['REMOTE_ADDR'],
                                           
$challenge_field,
                                           
$response_field);
        if (
$response->is_valid)
        
{
            
return TRUE;
        
}
        
else 
        
{
            $CI
->validation->recaptcha_error $response->error;
            
$CI->validation->set_message('recaptcha_matches''The %s is incorrect. Please try again.');
            return 
FALSE;
        
}
    }
    

5. Create a recaptcha.php in your config folder and add your public and private keys (the ones you got from registering) as seen below:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['recaptcha_public_key'"...";
$config['recaptcha_private_key'"..."

Example usage in a controller:

public function register()
    
{
        $this
->load->library('validation');
        
$this->load->helper('recaptcha');
        
        
$rules['username''trim|required|unique[users.username]';
        
$rules['password''required|password|matches[password_confirm]';
        
$rules['password_confirm''required';
        
$rules['email''trim|required|valid_email|unique[users.email]';
        
$rules['recaptcha_challenge_field''required|recaptcha_matches';
        
        
$this->validation->set_rules($rules);
        
        
$fields['username''username';
        
$fields['password''password';
        
$fields['password_confirm''password confirmation';
        
$fields['email''email';
        
$fields['recaptcha_challenge_field''answer to the security question';
        
        
$this->validation->set_fields($fields);
        
        if (
$this->validation->run() == FALSE)
        
{
            $this
->load->view('user_registration');
        
}
        
else 
        
{
            
echo 'Success!';
        
}
    } 

In the view, you can create the captcha form element by echoing <?= recaptcha() ?>
I can package all of this into a zip file upon request.

 
Posted: 19 October 2008 04:36 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-08-03
52 posts

Thanks a lot. Very cool. You can send me the zip in this email .(JavaScript must be enabled to view this email address). Also i want to publish this things in my blog with refer you.

Keep up this good work.

 
Posted: 29 October 2008 03:07 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2008-08-17
52 posts

hi, i got this problem

This reCAPTCHA key isn't authorized for the given domain. More info 

Do you know why and how to fix? I think it’s because reCAPTCHA system :-?

Note: i used public and private key both for subdomain and domain.

 Signature 

http://drimr.com

 
Posted: 31 October 2008 01:52 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2008-07-13
45 posts
Berserk - 29 October 2008 07:07 AM

hi, i got this problem

This reCAPTCHA key isn't authorized for the given domain. More info 

Do you know why and how to fix? I think it’s because reCAPTCHA system :-?

Note: i used public and private key both for subdomain and domain.

Have you been able to fix the problem? I’m not sure what the issue is.

Try to make sure your domain name matches and that you entered in your public / private keys in correctly into the config file.

 
Posted: 07 November 2008 07:19 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2008-08-17
52 posts

thanks, i tried with other domain and it works ! But look like their problem with subdomain.

 Signature 

http://drimr.com

 
Posted: 18 December 2008 02:19 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2008-12-10
42 posts

this is what i’m looking for. Would you like to send me the zip file ? I’m a little bit confuse with the validation, form_validation, and the view .. so i need to look the whole file ... ohh, almost forgot .. you can send to .(JavaScript must be enabled to view this email address)

 
Posted: 18 December 2008 10:18 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2008-07-13
45 posts

Hi PermanaJ, I wrote this helper for version 1.6 of CI. The current version of CI is now 1.7 which uses form_validation instead of validation.

Someone else modified it to work with version 1.7 and posted it here: http://ellislab.com/forums/viewthread/96365/

Good luck!

 
Posted: 18 December 2008 10:24 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2008-12-10
42 posts

Ohh .. im using 1.7 .. thank a lot for the information ...

but the code above work well with 1.7

 
Posted: 24 January 2009 12:34 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Joined: 2009-01-24
1 posts

Hi,
I am not a website admin, I am just trying to post a comment into a site that uses Capcha.
The Capcha works for everyone else, i.e. they can see an image and a field to enter what they see and are able to click sumbit, but not for me.

Instead, I get the same message “This reCAPTCHA key isn’t authorized for the given domain. More info”

I read the replies but don’t understand the cause of the problem, as I’m not very capcha savvy.. :(

can anyone help?

thank you smile

 
Posted: 02 July 2009 07:57 PM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2007-03-20
51 posts

I’ve made changes to make this work in CI 1.7.x

First, rename MY_Validation.php to MY_Form_validation.php

then change the code to match this:

<?php
if (!defined('BASEPATH')) exit ('No direct script access allowed');

class 
MY_Form_validation extends CI_Form_validation {

  
function MY_Form_validation() {
    parent
::CI_Form_validation();
  
}

  
function recaptcha_matches() {
    $CI 
= & get_instance();
    
$CI->config->load('recaptcha');
    
$public_key $CI->config->item('recaptcha_public_key');
    
$private_key $CI->config->item('recaptcha_private_key');
    
$response_field $CI->input->post('recaptcha_response_field');
    
$challenge_field $CI->input->post('recaptcha_challenge_field');
    
$response recaptcha_check_answer($private_key$_SERVER['REMOTE_ADDR']$challenge_field$response_field);
    
    if (
$response->is_valid{
      
return TRUE;
    
}
    
else {
      $this
->recaptcha_error $response->error;
      
$this->set_message('recaptcha_matches''The %s is incorrect. Please try again.');
      return 
FALSE;
    
}
  }

and thats it.
Your recapture should now work with the current form validation class.

 
Posted: 07 August 2009 08:38 AM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2009-08-05
58 posts
milka5000 - 24 January 2009 05:34 PM

Hi,
Instead, I get the same message “This reCAPTCHA key isn’t authorized for the given domain. More info”

Maybe you are using a key registered for specific domain name. If you want to use a key for multiple domain names you must create a new key that’s global. The global key can be used on multiple domain names smile

 Signature 

Personal Website:
http://www.ifadey.com

Photography on Flickr:
http://www.flickr.com/fadey86

 
Posted: 17 December 2009 09:47 AM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2009-03-03
115 posts

Why can’t I make it work ?

I have the reCaptcha displayed well in my page, but as soon as i click on the box i am redirected to the home page!
Please help me I have my problem explained here :
http://ellislab.com/forums/viewthread/128593/#684267

 Signature 

Yann
Web Developer
Portfolio

 
Posted: 03 December 2010 08:53 PM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Joined: 2010-12-03
2 posts

Damsel In reCaptcha Distress! Constructive Advice Sought

I have no idea what reCaptcha is about yet when I try to post or reply to certain ads on Craigslist that require typing the verification words into the box, no words or box pops up, just “Continue” which is supposed to pop up AFTER you type in the verification words. That’s when “reCaptcha Undefined” error messages pop up. This has gone on for well over a month. Going into CL’s peanut gallery discussion help forum has been futile (guesswork) at best. I have exhausted every measure that was suggested to me ie trying different browsers, downloading Firefox, resetting the Default settings in Internet options etc etc. People have even suggested scripting errors without offering any solutions. Ouff. I’ve even tried researching the net to try to find out what may be causing the problem and how to remedy it. Again, futility. The following is one of several different error messages: “‘pID’ is Undefined Line:3 postings.js Char:30 Code:0
URi: http//www.craigslist.org/js/postings.js” The one that comes up the most is the “reCaptcha Undefined”. Can anyone plz explain what is going on and walk me through how to remedy it? Plz send any responses to .(JavaScript must be enabled to view this email address) Thank you.

 
Posted: 07 April 2011 05:37 AM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Joined: 2011-03-02
5 posts

Hello guys,

I’m newbie in php/CI, could you please help me to understand how should I show reCAPTCHA in the view?

I have tried to add reCAPTCHA into the view as described in this topic, and here: http://code.google.com/intl/ru-RU/apis/recaptcha/docs/php.html

But reCAPTCHA always shown in the left top corner of the page, I suggest the issue is that <? ?> instructions are being processed with first priority and result is attached into the start of the response stream. But I do not understand how other guys placing reCAPTCHA into the any custom place of the view. Please help :`(

I’m using reCAPTCHA as described in this topic, not ajax.

Thanks in advance.

 
Posted: 11 April 2011 08:31 AM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Joined: 2011-03-02
5 posts

Please help. I have understood, that php code runs first and all html attached to the end of result of php.

How can I insert php generated html into the place holder in the CI template?

 
Posted: 18 April 2011 07:25 PM   [ Ignore ]   [ # 15 ]   [ Rating: 0 ]
Joined: 2011-04-18
1 posts

I’ve been using the code you gave in this forum, but I still don’t understand why not display anything in my Smarty template? How to use the recaptcha in CI (2.0.2) + Smarty (3.0.7), Thank you in advance.

 
1 of 2
1