0
votes

I am trying to add google's reCAPTCHA in my CakePHP application's login and registration pages. But it showing following error:

Fatal Error Error: Call to undefined function recaptcha_check_answer()
File: C:...\app\Controller\UsersController.php

in the UsersController , i have following line which is indicated in the error message.

$recaptchaResp = recaptcha_check_answer(Configure::read("Recaptcha.privateKey"),
                            $_SERVER["REMOTE_ADDR"],

                            $this->params['form']["recaptcha_challenge_field"],
                            $this->params['form']["recaptcha_response_field"]);

Could anyone tell me please what 's wrong in this code ? is it missing any file like recaptchalib ? if so, where can i get this library for CakePHP 2.5.1 ?

1
Just read the error message and you know what is wrong? If you read it but didn't understood it just Google it or check the php manual. - floriank
You answered your own question: "Error: Call to undefined function recaptcha_check_answer()". - drmonkeyninja
i already googled this problem, but could not find any solution. there are some suggestion about how to add reCAPTCHA in PHP application. but it is not precise about how to integrate it in application which is developed in CakePHP 2.5.1 (PHP 5.3.28). it seems there are different methods to add this plugin. - Tom
@burzum, is php manual applicable for CakePHP 2.5.1 ? - Tom
@Tom you haven't included where that function is declared. i.e. according to the docs for that you're missing require_once('recaptchalib.php'); or equivalent; writing an app with CakePHP doesn't fundamentally change that your app is written in ... PHP. - AD7six

1 Answers

1
votes

There is a plugin for google reCaptcha in cakephp. You can download it from Here

To use the recaptcha plugin its required to include the following two lines in your /app/Config/bootstrap.php file.

Configure::write('Recaptcha.publicKey', 'public-api-key');
Configure::write('Recaptcha.privateKey', 'private-api-key');

Controllers that will be using recaptcha require the Recaptcha Component to be included. Through inclusion of the component, the helper is automatically made available to your views.

public $components = array('Recaptcha.Recaptcha');

In the view simply call the helpers display() method to render the recaptcha input:

echo $this->Recaptcha->display();

To check the result simply do something like this in your controller:

if ($this->request->is('post')) {
    if ($this->Recaptcha->verify()) {
        // verified
    } else {
        // display the error
    }
}