javascript - jQuery Real Person - 总是失败

标签 javascript php jquery validation captcha

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

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>

最佳答案

Craig Jacobs 说得好,这与您指出的问题相同。我也遇到了同样的事情,并通过如下所示的更改解决了问题:

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;
    }
    .....
    .....
}

希望它也能帮助到其他人。

关于javascript - jQuery Real Person - 总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20016398/

相关文章:

javascript - Android 浏览器复选框标签不可点击

jquery - 单击子菜单时将 WordPress 菜单设置为事件状态

javascript - RTCDataChannel send() 是同步/阻塞调用吗?

javascript - 循环完成后在 JS 中调用函数?

javascript - express 或 express 发电机 : do i need both?

php - 来自 php 的 AJAX 响应文本不起作用

javascript - 如何将选定的值作为额外参数传递给ajax调用

php - 如何创建一个对大型动态数据集中的标签进行计数的性能系统

php - MySQL查询唯一用户和最小分值

javascript - 使用包含英国格式日期的字符串设置 new Date()