Welcome Guest, Not a member yet? Register   Sign In
Implementing reCAPTCHA in CI
#1

[eluser]Unknown[/eluser]
Hello all,

I intend this as a guide to those struggling to add reCAPTCHA to CI. Here's how I was able to do it:

1. After registering, I downloaded the reCAPTCHA PHP library at http://code.google.com/p/recaptcha/downl...lib-Latest.
2. I renamed recaptchalib.php in that ZIP file to recaptcha_helper.php and dropped it in my application/helpers directory.
3. I used this code in my view (replacing
Code:
PUBLIC_KEY
with my own key that reCAPTCHA generated):

Code:
$CI =& get_instance();
$CI->load->helper('recaptcha');
$publickey = "PUBLIC_KEY";
echo recaptcha_get_html($publickey);

(Obviously if you're loading this in a controller or model, the top two $CI lines can be replaced with
Code:
$this->load->helper('recaptcha');
)

4. In the validation method of my controller, I added this code (replacing
Code:
PRIVATE_KEY
with my private key):

Code:
$this->load->helper('recaptcha');
$privatekey = "PRIVATE_KEY";
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $this->input->post('recaptcha_challenge_field'), $this->input->post('recaptcha_response_field'));

if (!$resp->is_valid)
{
// What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
    die('Success!');
}

That's it! So far it seems to be working. Hope this helps!
#2

[eluser]Corbee[/eluser]
Thanks
#3

[eluser]tkyy[/eluser]
i find the best way to implement recaptcha is to actually do a curl call to the noscript version of the page (using your public key as the get parameter) and then parsing the challenge id out of the text. you then display the challenge id using the recaptcha url to get images (the parameter in that url is "c=").

this way you can avoid having a ton of crappy "powered by recaptcha" images all over your page and you can display the image itself in any way you see fit. make sure to store the challenge id as a hidden variable and pass it to your form submission function for validating.

here is an exact example of how i do it on a live social networking site

Code:
//grab the recaptcha page
$page = file_get_contents('http://api.recaptcha.net/challenge?k=recaptcha_public_key');

//split the document into lines
$lines = explode("\n",$page);

//find the line containing the challenge code
foreach($lines as $line){
    if(strstr($line,"challenge : '")){
        $url = str_replace("challenge : '",null,$line);
        $url = str_replace("',",null,$url);
        $url = trim($url);
        break;
    }
}

//create the html for displaying the image and store the challenge code into a variable
$recaptcha = '<img src="http://api.recaptcha.net/image?c='.$url.'" title="Prove you are human!">';
$challenge = $url;
?&gt;

i find this is better like i said because you can manipulate it anyway you want.




Theme © iAndrew 2016 - Forum software by © MyBB