php验证码不工作

php验证码不工作,php,forms,validation,Php,Forms,Validation,我使用一个旧的random()函数为我在web上找到的AJAX评论系统创建验证代码(源代码位于) 背后的想法很简单: function Random() { $chars = "ABCDEFGHJKLMNPQRSTUVWZYZ23456789"; srand((double)microtime()*1000000); $i = 0; $pass = '' ; while ($i <= 4) { $num = rand() % 32; $tmp = substr($chars, $num,

我使用一个旧的random()函数为我在web上找到的AJAX评论系统创建验证代码(源代码位于)

背后的想法很简单:

 function Random()
{
$chars = "ABCDEFGHJKLMNPQRSTUVWZYZ23456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 4)
{
$num = rand() % 32;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$random_code = Random(); 
所以我写了这个:

if(!($data['security_code'] = filter_input(INPUT_POST,'security_code',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
{
$errors['security_code'] = 'You did not enter the validation code.';
}
elseif(!($data['security_code'] = $randomness))
{
$errors['security_code'] = 'You entered the validation code incorrectly. Please note that it is case sensitive.';
} 
但是,当我在验证代码文本字段中插入随机文本(在上自行测试)后单击提交按钮时,我总是收到“您输入的验证代码不正确”消息

print\r($\u POST)给出一个空数组,然后在我单击submit后脚本挂起: 排列 ( )

我错过了什么?原始验证码在验证过程中的某个点丢失(第3和第4个代码块)。 提前感谢,而不是:

<input name="randomness" type="hidden" id="randomness" value="<?php $random_code; ?>"> 
elseif(!($data['security_code'] = $randomness))
{
$errors['security_code'] = 'You entered the validation code incorrectly. Please note that it is case sensitive.';
}
也许是这样:

elseif($data['security_code'] != $randomness) {
   $errors['security_code'] = 'You entered the validation code incorrectly. Please note that it is case sensitive.';
} 
另外,从何处
$data
获取其值$\u发布,$\u获取
print\u r()
it和
$\u请求点亮

看到您的代码后,我发现静态函数validate不知道变量
$randomanness
!在submit.php中,您正在进行以下调用:

$arr=array()
$validates=Comment::validate($arr)

函数
validate
不知道变量
$randomanness
的任何信息,除非您将此类信息传递给它-它位于不同的范围内

尝试修改上述代码,如下所示:

    $arr = array(); // no change here  

    $randomness = isset($_POST['randomness']) ? $_POST['randomness'] : '';   
    // Check for empty randomness before you validate it in Comment::validate
    // so that you donot verify for '' == '' there. 

    $validates = Comment::validate($arr, $randomness);
并按如下方式更改验证功能:

    public static function validate(&$arr, $randomness)
    {
我知道这不是一个优雅的解决方案——这需要你自己学习更多的东西,这只是给你指路……

让我知道进展如何

你认为
if(!$blah['A']='B')
if($blah['A']!='B')
不同吗?我会说不。但是我已经尝试过另一种方法,没有任何改变。哈哈:你能告诉我你在最后一段代码中想做什么吗?只是开玩笑。。。最后一块检查验证代码字段是否为空(它已经工作正常,filter_input()函数不会影响最后一块代码的第二部分,因此我没有在这里编写它),或者用户插入了错误的代码。您知道平等性检查是使用
==
完成的吗=而不是使用
$废话['blah']=“废话”
?通过这一点,您正在分配并检查分配是否成功:)谢谢,这与@Shrinath正确地告诉我的是同一件事。不幸的是,一切都没有改变。此外,当验证程序检查验证代码时,验证代码似乎实际上丢失了。我通过在输出中显示$randomness变量来检查该变量的内容:
errors['security\u code']='
您输入了'.$randomness'。验证代码不正确。'并且它似乎是空的(也就是说,我得到
您输入的验证代码不正确。[…]
而不是
您输入的验证代码不正确。[…]
)随机();函数工作正常,所以服务器中没有空$randomanness的方法;但是“Comment::validate_text”呢?它与脚本的另一部分有关,该部分使用filter_input()函数检查空文本字段,但是它工作得很好(此外,我已经尝试暂时删除它并跳过空文本字段检测,但没有做任何更改),谢谢。从代码中的所有输入POST和没有输入GET来看,它肯定是$POST然后,称为echo“”;打印(邮政美元);回声“;并将结果代码放在原始帖子(编辑)中查看。您好,谢谢您的努力。不幸的是,它不起作用,即使我插入了正确的代码,它也会显示“您插入的代码不正确”。从错误日志中,我得到:
[02-Jun-2012 18:42:08 UTC]PHP警告:注释缺少参数2::第174行/home/flapanec/public\u html/guestbook/Comment.class.PHP中的validate_text()。这将是
私有静态函数validate_text($str,$randomanness)
为什么要发送
$randomanness
来验证_text?我只想让它用于
公共静态函数验证(&$arr,$randomanness)
当你想在晚了、累了的时候编辑php代码时,你会得到它,而不是上床睡觉。。。抱歉,我显然将
$randomness
传递给了错误的函数。它工作得很好,它帮助我了解了整个过程是如何工作的,所以非常感谢!与此同时,昨天我添加了Facebook Connect支持、类似Twitter的AJAX按钮(又名“加载更多评论”),以及“位置”文本字段的自动地理定位。如果你能第一个在新版留言簿上签名,我将不胜荣幸:)太好了:)很高兴知道它能工作,所以请投票并接受答案,让人们知道这个问题已经解决,以及它是如何解决的:)
elseif($data['security_code'] != $randomness) {
   $errors['security_code'] = 'You entered the validation code incorrectly. Please note that it is case sensitive.';
} 
    $arr = array(); // no change here  

    $randomness = isset($_POST['randomness']) ? $_POST['randomness'] : '';   
    // Check for empty randomness before you validate it in Comment::validate
    // so that you donot verify for '' == '' there. 

    $validates = Comment::validate($arr, $randomness);
    public static function validate(&$arr, $randomness)
    {