Protected variable not being set?
Posted: Wed Sep 23, 2009 11:31 am
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;
};
}
};