简体   繁体   English

reCaptcha不能在php中按预期工作

[英]reCaptcha isnt working as intended in php

I'm trying to add reCaptcha to my registration page, but i cant seem to get it to work, it keeps displaying the message that the data was entered incorrectly, the code is below: 我正在尝试将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>

I have a feeling that my problem is in ($verify!=is_valid) but i am not sure. 我感到我的问题出在($ verify!= is_valid),但我不确定。 Would appreciate any help. 将不胜感激。

Per the docs found here , it looks like you're not using the return value of recaptcha_check_answer() properly: 根据此处找到的文档,您似乎没有正确使用recaptcha_check_answer()的返回值:

recaptcha_check_answer returns an object that represents whether the user successfully completed the challenge. recaptcha_check_answer返回一个表示用户是否成功完成挑战的对象。
If $resp->is_valid is true then the captcha challenge was correctly completed and you should continue with form processing. 如果$ resp-> is_valid为true,则验证码质询已正确完成,您应继续进行表单处理。

Instead of $verify != is_valid , you should use the is_valid value on the returned object: 您应该在返回的对象上使用is_valid值,而不是$verify != is_valid

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM