我目前正在使用reCaptcha库here在我的网站上运行reCaptcha,它在Debian 6.5上使用LAMP版本2.2.22和PHP版本5.4.39-0 + deb7u2。我的PHP代码如下。
// Recaptcha check
$secret = "SomeAlphaNumericGibberish";
$response = null;
$reCaptcha = new ReCaptcha($secret);
if ($reCaptcha ==NULL){
echo "Null pointer " . "<br>";
} else{
echo "Valid pointer " . "<br>";
}
if ($_POST["g-recaptcha-response"]) {
echo "Remote IP: ". $_SERVER["REMOTE_ADDR"] . "<br>";
$response = $reCaptcha->verifyResponse(
$_SERVER["REMOTE_ADDR"],
$_POST["g-recaptcha-response"]
);
}
if ($response != null && $response->success) {
echo "Welcome " . $_POST["username"] . "!" . "<br>";
} else if ($response == null) {echo "Null pointer " . "<br>";}
else {
echo "Bad response" . "<br>";
foreach ($response->getErrorCodes() as $code) {
echo $code . "<br>";
}
echo "End of error codes" . "<br>";
}
输出如下
Valid pointer
Remote IP: 192.168.1.4
Bad response
然而,之后我没有得到任何输出。我跑了
sudo cat /var/log/apache2/error.log
得到了
PHP Fatal error: Call to undefined method ReCaptchaResponse::getErrorCodes()
我无法找到有关此错误消息的任何讨论。
答案 0 :(得分:2)
根本不需要使用该库。它增加了不必要的重量。
假设您正在使用最新的recaptcha js库并且您的html已配置,这将有效:
$recaptchasecret = 'adslfjasdlf';
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify?secret='.
$recaptchasecret.'&response='.$_POST['g-recaptcha-response'].'&remoteip='.$_SERVER['REMOTE_ADDR'];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $recaptcha_url,
CURLOPT_USERAGENT => 'MyWebsite.Com'
));
$response = curl_exec($curl);
curl_close($curl);
unset($curl);
$auth = json_decode($response,false);
if(!$auth->success){
throw new exception('Captcha Failed!');
}