Protected variable not being set?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Protected variable not being set?

Post 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;
        };
    }
};
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Protected variable not being set?

Post 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?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Protected variable not being set?

Post by Darhazer »

Are you sure that later you are still using the same instance of System_ReCaptcha?
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: Protected variable not being set?

Post 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 !
Post Reply