2013-12-17 69 views
0

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

<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

什么是'is_valid'?是不是似乎在你的脚本中定义在这里;另外,它没有用'$'作为前缀,所以我假设它是一个常量? – newfurniturey

+0

什么是'is_valid'?它似乎不是一个函数调用,并且没有该名称的变量,所以我假设P​​HP将其解释为一个常量 – andrewsi

+0

@newfurniturey - argh!请腾出我的头! – andrewsi

回答

0

%的文档中发现here,它看起来像你不使用的recaptcha_check_answer()返回值正常:

recaptcha_check_answer返回表示 用户是否已成功完成挑战的对象。
如果$ resp-> is_valid为true,那么验证码挑战已正确完成,您应该继续进行表单处理。

相反的$verify != is_valid,你应该对返回的对象使用is_valid值:

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