【发布时间】:2013-11-29 18:00:38
【问题描述】:
无论我输入什么(即使是正确的验证码),我总是得到不匹配的输出。我尝试回显这些值(如果您输入正确的代码,它们应该匹配)。我总是得到这样的东西:
6952304285049
-1247767175
- 我正在使用 jquery-1.10.2.min.js (并在我的标题中将它与 realperson.js 文件一起链接) http://gfishdesigns.com/COMP2920/_COMPLETED/Assignment%202/SignUp.php
这是我的代码(我也在做一些其他的验证):
<?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