【问题标题】:jQuery Real Person - always failsjQuery Real Person - 总是失败
【发布时间】:2013-11-29 18:00:38
【问题描述】:

无论我输入什么(即使是正确的验证码),我总是得到不匹配的输出。我尝试回显这些值(如果您输入正确的代码,它们应该匹配)。我总是得到这样的东西:

6952304285049
-1247767175

这是我的代码(我也在做一些其他的验证):

<?php
include 'Header.php';
include 'Database.php';
?>
<script type="text/javascript">
    $(function() {
        $('#defaultReal').realperson();
    });
</script>

<h1>Sign Up</h1>

<?php

if ($_POST){

    $username = $_POST['username'];
    $password = $_POST['password'];
    $check = '';

    //validate CAPTCHA
    function rpHash($value) { 
        $hash = 5381; 
        $value = strtoupper($value); 
        for($i = 0; $i < strlen($value); $i++) { 
        $hash = (($hash << 5) + $hash) + ord(substr($value, $i)); 
        } 
        return $hash; 
    } 
      if (rpHash($_POST['defaultReal']) == $_POST['defaultRealHash']) { ?>
        <p class="accepted">You have entered the "real person" value correctly and the form has been processed.</p>
<?php 

    //if username is not blank
    if($username != ''){

        //check if username exists already
        $query = "SELECT username FROM tbl_user;";
        $result = mysql_query($query) or die(mysql_error());
        while ($record = mysql_fetch_row($result))
        {
            foreach($record as $field)
            {
                if($field == $username){
                    //if user exists, dont let them add same user
                    $error_message_username = 'username already used; choose a unique name';
                }
                else{
                    $check = 'pass';
                }
            }
        }   
    }else{
        $error_message_username = 'username cannot be blank';
    }

    //if password is not blank
    if($password != ''){    
        $error_message_password = '';

        // encrypt password
        $encrypted_password = md5($password);   

        if($check == 'pass'){
            //set username and password into database
            $query = "INSERT INTO tbl_user VALUES('','".$username."','".$encrypted_password."');";
            $result = mysql_query($query) or die(mysql_error());
        }
    }else{
        $error_message_password = 'password cannot be blank';
    }



    } else { ?>
            <p class="rejected">You have NOT entered the CAPTCHA value correctly and the form has been rejected.</p>
    <?php 
echo rpHash($_POST['defaultReal']) . '<br/>';
echo $_POST['defaultRealHash'];

}

}    

?>

<form method="post" action="SignUp.php">
  <p>
    E-Mail:
    <input type="text" class="required email" id="username" name="username">
    <?php 
    if ( $error_message_username != '' ) {
        print "$error_message_username";
    }
    ?>
  </p>
  <p>
    Password:
    <input type="text" name="password">
            <?php 
    if ( $error_message_password != '' ) {
        print "$error_message_password";
    }
    ?>
  </p>
  <p>
    CAPTCHA:
    <input type="text" id="defaultReal" name="defaultReal">
  </p>
  <p>
    <button class="mybutton" type="submit" value="Sign Up">Sign Up</button>
  </p>
</form>

【问题讨论】:

  • mysql_ 已弃用。最好使用mysqli_ 或 PDO。此外,您还容易受到微不足道的 SQL 注入攻击。祈祷某人的用户名不是'); DROP database --。使用准备好的语句(mysql_ 不支持)将解决这个问题。此外,通过获取所有条目并循环遍历它们来检测数据库中的条目是否存在有点愚蠢(缓慢)(并且容易受到竞争条件的影响)。最好让数据库通过唯一约束来完成(预检查很好但还不够)
  • 你从哪里得到用户需要重新输入的验证码值? AFAIK 这必须首先存储到会话中进行验证 - 这就是大多数验证码库的工作方式?
  • 我确信我这样做的方式很愚蠢/很慢,因为这是 PHP 课程中的家庭作业,所以我仍在学习如何做这些事情。从我为 realperson.js 下载的 .html 文件中的工作示例中可以看出,不涉及任何会话。它不会通过这个抓取该人的验证码条目吗? $_POST['defaultRealHash']
  • 我实际上可能有 defaultReal 和 defaultRealHash。让我切换并让您知道是否可以解决它...
  • 啊! rpHash 函数 (php) 使用 >> 位运算符,它在 32 位和 64 位机器上的响应显然不同。因此,请在 64 位 php 下使用网站上提供的其他 php 函数。我没有对此进行彻底调查,但它适用于我的演示服务器 (Mac) 和生产服务器 (Cent-OS) PHP 5.4.x x86_64

标签: javascript php jquery validation captcha


【解决方案1】:

克雷格·雅各布斯说得好,这与您指出的问题相同。我也遇到了同样的事情,并通过如下所示的更改来解决:

function rpHash($value) {
    $hash = 5381;
    $value = strtoupper($value);
    for($i = 0; $i < strlen($value); $i++) {
        $hash = (leftShift32($hash, 5) + $hash) + ord(substr($value, $i));
    }
    return $hash; }

function leftShift32($number, $steps) {
    $binary = decbin($number);
    $binary = str_pad($binary, 32, "0", STR_PAD_LEFT);
    $binary = $binary.str_repeat("0", $steps);
    $binary = substr($binary, strlen($binary) - 32);
    return ($binary{0} == "0" ? bindec($binary) :
        -(pow(2, 31) - bindec(substr($binary, 1)))); 
}

if (isset($_POST['submit'])) {

    .....
    if (rpHash($_POST['defaultReal']) != $_POST['defaultRealHash']) {
        echo "Invalid contact request, please try again with correct verification code...";
        exit;
    }
    .....
    .....
}

希望它也能帮助其他人。

【讨论】:

    【解决方案2】:

    提供了两个版本的 php rpHash 函数,一个用于 32 位 PHP,一个用于 64 位 PHP。运行 phpinfo 并确保您使用的是本页 http://keith-wood.name/realPerson.html 上提供的正确版本的函数。此处使用的按位函数将在 32 位和 64 位机器上返回不同的值。看到这个页面:http://www.php.net/manual/en/language.operators.bitwise.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-06
      • 2016-05-10
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多