reCaptcha在php中没有按预期工作


reCaptcha isnt working as intended in php

我正试图将reCaptcha添加到我的注册页面,但我似乎无法使其工作,它一直显示数据输入错误的消息,代码如下:

<div align="center">    
<?PHP
if (isset($_POST['submit']))
{
$username = $_POST["username"];
$password = $_POST["password"];
$password1 = $_POST["password1"];
if(empty($username)) die(print '<script> alert ("Enter Username"); window.location="registration.php"; </script>');
if(empty($password)) die(print '<script> alert ("Enter Password"); window.location="registration.php"; </script>');
if($password != $password1) die(print '<script> alert ("Password doesn''t match"); window.location="registration.php"; </script>'); 
require_once('recaptchalib.php'); // reCAPTCHA Library
$privkey = "6Ld27usSAAAAAKiYor8lnBs9fb6HOd1IK5JnTCyL"; // Private API Key
$verify = recaptcha_check_answer($privkey, $_SERVER['REMOTE_ADDR'],   $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);
if ($verify!=is_valid) { 
  echo "You did not enter the correct Captcha.  Please try again.";
}
else {

$file = file_get_contents("data.txt");
$string = "$username||$password";
if(!strstr($file, "$string"))
{
$myFile = "data.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$username||$password'n";
fwrite($fh, $stringData);
print '<script> alert ("Registration Complete"); window.location="index.php"; </script>';
fclose($fh);
}
else
{
echo "Sorry the username: <b>$username</b> is already registered. Please use diferent username.";
}
}
}
?>
</div>
<!doctype html>
<html>
<head>
<title>Registration</title>
</head>
<body>
<div id="container" style="width:500px; height:500px; border: 2px solid black; margin:auto">
<?php include "header.php"; ?>
<div id="content" style="background-color:#EEEEEE; width:500px; height:400px; float: left">
<br>
<form align="center" method="post" action="registration.php" >
Username:
<input type="text" name="username" />
<br/>
<br/>
Password:
<input type="password" name="password" />
<br/>
<br/>
Confirm:
<input type="password" name="password1" />
<br/>
<br/>
<?php
require_once('recaptchalib.php'); // reCAPTCHA Library
$pubkey = "6Ld27usSAAAAAB9Zq67L28CqywwEn9RZ_7bFthm7"; // Public API Key
echo recaptcha_get_html($pubkey); // Display reCAPTCHA
?>
<input type="submit" value="Register" name="submit" />
</form>
</div>
<?php include "footer.php"; ?>
</div>
</body>
</html>

我觉得我的问题在($verify!=is_valid)中,但我不确定。如果有任何帮助,我将不胜感激。

根据此处找到的文档,您似乎没有正确使用recaptcha_check_answer()的返回值:

repatcha_check_answer返回一个对象,该对象表示用户成功完成了挑战
如果$resp->is_valid为true,则captcha挑战已正确完成,您应该继续表单处理。

您应该在返回的对象上使用is_valid值,而不是$verify != is_valid

if ($verify->is_valid) {
    echo "You did not enter the correct Captcha. Please try again.";
} else {
    ...