Welcome Guest, Not a member yet? Register   Sign In
Recaptcha and CI
#1

[eluser]AtlantixMedia[/eluser]
hello,

I'm trying to incorporate a recaptcha box in a registration system but I'm getting an 'undefined property' error. in my controller I have:

Code:
class Register extends Controller {


    //recaptcha
    var $errCaptcha = null;
    var $resp = null;

    //===============================================
    function index()
    //===============================================
    {
    $this->load->helper('recaptchalib');
        //other stuff
    }    



//===============================================
    function ValidateCaptcha()
    //===============================================
    {
    $resp = null;    $errCaptcha = null;    $ret = 0;
    
    $resp = recaptcha_check_answer($this->config->item('CaptchaPrivateKey'),$_SERVER["REMOTE_ADDR"],$_POST["recaptcha_challenge_field"],$_POST["recaptcha_response_field"]);
    if ($resp->is_valid) {     }
    else                       {    $errCaptcha = $resp->errCaptcha;     $ret = 1;    }
    
    $this->resp = $resp;
    $this->errCaptcha = $errCaptcha;
    return $ret;
    
    }

}

the error says 'undefined property: errCaptcha and points to:
Code:
else                       {    $errCaptcha = $resp->errCaptcha;

In recaptchalib_helper.php the function being called by ValidateCaptcha is:

Code:
function recaptcha_check_answer ($privkey, $remoteip, $challenge, $response)
{
    if ($privkey == null || $privkey == '') {
        die ("To use reCAPTCHA you must get an API key from <a href='http://recaptcha.net/api/getkey'>http://recaptcha.net/api/getkey</a>");
    }

    if ($remoteip == null || $remoteip == '') {
        die ("For security reasons, you must pass the remote ip to reCAPTCHA");
    }

    
    
        //discard spam submissions
        if ($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0) {
                $recaptcha_response = new ReCaptchaResponse();
                $recaptcha_response->is_valid = false;
                $recaptcha_response->error = 'incorrect-captcha-sol';
                return $recaptcha_response;
        }

        $response = _recaptcha_http_post (RECAPTCHA_VERIFY_SERVER, "/verify",
                                          array (
                                                 'privatekey' => $privkey,
                                                 'remoteip' => $remoteip,
                                                 'challenge' => $challenge,
                                                 'response' => $response
                                                 )
                                          );

        $answers = explode ("\n", $response [1]);
        $recaptcha_response = new ReCaptchaResponse();

        if (trim ($answers [0]) == 'true') {
                $recaptcha_response->is_valid = true;
        }
        else {
                $recaptcha_response->is_valid = false;
                $recaptcha_response->error = $answers [1];
        }
        return $recaptcha_response;

}


I'm sure this is because an object ($recaptcha_response = new ReCaptchaResponse()Wink is being created in recaptchalib_helper.php and is not being properly handled within the controller. I'm just starting out working with classes. please help. thanks you
#2

[eluser]Sarfaraz Momin[/eluser]
I have successfully implemented it on one of my websites in the contact us form. The code goes as below. I hope you already have recapchalib.php file. So I am providing the implementation code.

Show Recaptcha in your form.

Code:
$this->load->helper('recaptchalib');
$publickey = "jioeuiroudkljfdklmcmkldmcklsdmkd";
$recaptcha = recaptcha_get_html($publickey);

Validate recaptcha from the submitted form.

Code:
$this->load->helper('recaptchalib');
$privatekey = "dsdsdadaskjldlaskjdlkasjdklasjkd";
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
echo "success";
       }
    else
      {    
echo "failed";
}

The keys ofcourse are invalid so you might have to change those.
#3

[eluser]AtlantixMedia[/eluser]
hey thanks for your prompt response. yes, I've been using recaptcha on all my websites built on "procedural coding". so I see you do not pass an error back when calling:

$recaptcha = recaptcha_get_html($publickey);

I know that argument is optional, but is there a way to get that error string back (incorrect-captcha-sol) in an OO environment as I used to do in a class-less function?

this is how I would handle recaptcha in my previous procedural projects:

Code:
//VALIDATE CAPTCHA
$resp = recaptcha_check_answer ($GLOBALS['CaptchaPrivateKey'],$_SERVER["REMOTE_ADDR"],$_POST["recaptcha_challenge_field"],$_POST["recaptcha_response_field"]);
if ($resp->is_valid) {     }
else                       {    $error = $resp->error;            $ErrCounter++;    }

thanks!

P.S.: by the way, a quick question unrelated to recaptcha: I'm trying to access a config item from within a helper file but I get a

Fatal error: Call to a member function on a non-object in.....

any ideas on how to fix this? I'm simply using $this->config->item('WebsiteTitle'); in a helper file function. thanks
#4

[eluser]Crimp[/eluser]
Did you remember to load the configs, as per the Config class?
#5

[eluser]AtlantixMedia[/eluser]
yes. that particolar config file is autoloaded

Code:
$autoload['config'] = array('myconfig/config');
#6

[eluser]Unknown[/eluser]
Hi, I'm trying to use Recaptcha with my application and am running in to some issues. I followed all of the instructions at http://codeigniter.com/wiki/ReCAPTCHA/ and can get the recaptcha to load correctly, but am not able to get the code to validate that what the user entered is correct. Does anyone have any sample code of how they get function recaptcha_check_answer to run correctly? Any help is greatly appreciated.




Theme © iAndrew 2016 - Forum software by © MyBB