任何人都可以看到此登录脚本有什么问题:
public function login($username, $pass, $remember) {
// check username and password with db
// else throw exception
$connect = new connect();
$conn = $connect->login_connect();
// check username and password
$result = $conn->query("select * from login where
username='".$username."' and
password=sha1('".$pass."')");
if (!$result) {
throw new depException('Incorrect username and password combination. Please try again.');
} else {
echo $username, $pass;
}
解释:
目前脚本允许任何东西通过。换句话说,对于传递给它的任何用户名和密码,查询都返回 true。我已将 echo 语句作为检查 - 显然脚本将在正常情况下继续!
我知道连接类和login_connect方法正在工作,因为我在正常工作的注册脚本中使用它们。depException 只是 Exception 类的扩展。
函数 login() 是包含正常工作的 register() 的同一类的一部分。
我知道这两个变量($username 和 $pass)正在进入函数,因为 echo 语句正在准确地输出它们。(脚本的这一部分不需要 $remember 变量。它稍后用于记住我的过程)。
我难住了。请帮忙!
更新
感谢您的回复。我对查询返回的内容感到困惑。完整的脚本会检查返回的行数,这是应该进行检查的地方。现在一切正常,除了我的记住我功能。也许有人可以帮忙?!?!这是完整的脚本:
public function login($username, $pass, $remember) {
// check username and password with db
// else throw exception
$connect = new connect();
$conn = $connect->login_connect();
// check username and password
$result = $conn->query("select * from login where
username='".$username."' and
password=sha1('".$pass."')");
if (!$result) {
throw new depException('Incorrect username and password combination. Please try again.');
}
if ($result->num_rows>0) {
$row = $result->fetch_assoc();
//assign id to session
$_SESSION['user_id'] = $row[user_id];
// assign username as a session variable
$_SESSION['username'] = $username;
// start rememberMe
$cookie_name = 'db_auth';
$cookie_time = (3600 * 24 * 30);*/ // 30 days
// check to see if user checked box
if ($remember) {
setcookie ($cookie_name, 'username='.$username, time()+$cookie_time);
}
// If all goes well redirect user to their homepage.
header('Location: http://localhost/v6/home/index.php');
} else {
throw new depException('Could not log you in.);
}
}
非常感谢您的帮助。
更新 2!
感谢您的帮助,我已经完成了这个脚本的主要部分。但是,最后的记住我仍然不想工作。有人可以帮我解决吗?$username、$pass 和 $remember 都是我在将它们传递给函数之前分配的短变量名,以节省每次写入 $_POST['username'] 等。$remember 指的是一个复选框。