Page 1 of 1

Protected variable not being set?

Posted: Wed Sep 23, 2009 11:31 am
by Cirdan
I have a wrapper class for ReCAPTCHA. If the captcha is invalid, I want to keep the error in a protected variable. Under the if(!$result->is_valid) if I echo the $result->error it prints out the error correctly. However, it does not set $this->_error to the error value. So later when I want to retrieve the error, it returns nothing.

Code: Select all

<?php
 
require_once "recaptchalib.php";
 
// Custom ReCAPTCHA wrapper
class System_ReCaptcha {
    protected $_publicKey;
    protected $_privateKey;
    protected $_error;
    
    function __construct($publicKey, $privateKey) {
        $this->_publicKey = $publicKey;
        $this->_privateKey = $privateKey;
        $this->_error = '';
    }
    
    function getHtml() {
        return recaptcha_get_html($this->_publicKey);
    }
    
    function checkAnswer($challenge, $response) {
        $result = recaptcha_check_answer($this->_privateKey,
                                         $_SERVER['REMOTE_ADDR'],
                                         $challenge,
                                         $response);
                                         
        if(!$result->is_valid) {
            $this->_error = $result->error;
            return false;
        } else {
            return true;
        }
        
    }
    
    public function getError() {
        echo $this->_error;
        switch($this->_error) {
          case 'invalid-site-public-key':
          case 'invalid-site-private-key':
                return 'Invalid public or private key';
                break;
          case 'invalid-request-cookie':
                return 'Invalid challenge';
                break;
          case 'incorrect-captcha-sol':
                return 'Captcha did not match';
                break;
        };
    }
};

Re: Protected variable not being set?

Posted: Wed Sep 23, 2009 11:39 am
by Mark Baker
How are you trying to retrieve the error later?
I don't see any method in your class that will allow you to access it from outside?

Re: Protected variable not being set?

Posted: Wed Sep 23, 2009 1:54 pm
by Darhazer
Are you sure that later you are still using the same instance of System_ReCaptcha?

Re: Protected variable not being set?

Posted: Wed Sep 23, 2009 10:12 pm
by Cirdan
Darhazer wrote:Are you sure that later you are still using the same instance of System_ReCaptcha?
Yes, right before I was going to submit my post, that popped into my head. But I am using the same instance.

Code: Select all

               if(!empty($formValues['recaptcha_challenge_field']) &&
                   !empty($formValues['recaptcha_response_field'])) {
                       if(!$recaptcha->checkAnswer($formValues['recaptcha_challenge_field'],
                                                  $formValues['recaptcha_response_field'])) {
                        $errors['captcha'][] = $recaptcha->getError();                              
                        }
                } else {
                       $errors['captcha'][] = 'Missing captcha values';
                }
EDIT: Error was that my if statement above was missing an !