我正在尝试将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),但我不确定。非常感谢任何帮助。
答案 0 :(得分:0)
根据找到的文档here,您似乎没有正确使用recaptcha_check_answer()
的返回值:
recaptcha_check_answer 会返回一个表示是否的对象 用户成功完成了挑战 如果 $ resp-&gt; is_valid 为true,则验证码质询已正确完成,您应该 继续进行表格处理。
而不是$verify != is_valid
,您应该在返回的对象上使用is_valid
值:
if ($verify->is_valid) {
echo "You did not enter the correct Captcha. Please try again.";
} else {
...