下面是我的简单login.php代码,我用
创建了一个用户usermail:admin userpass:admin123
但每当我尝试使用管理员帐户登录时,它会忽略if语句并打开homepage.php,可能是什么问题,谢谢。
<?php
$connection = mysql_connect("localhost","user","password") or die("Could not connect to the database");
mysql_select_db("nisbet", $connection);
error_reporting(0);
if($_POST['login']){
if($_POST['usermail'] && $_POST['userpass']){
$usermail = mysql_real_escape_string($_POST['usermail']);
$userpass = mysql_real_escape_string(hash("sha512",$_POST['userpass']));
$user = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `usermail` = '$usermail'"));
if($user == 0){
die("User does not exits <a href='index.php'>← Back</a>");
}
if($user['userpass'] != $userpass){
die("Incorrect password! <a href='index.php'>← Back</a>");
}
//die("You are now logged in as $usermail !");
if($user['usermail'] == 'admin' && $user['userpass'] == 'admin123'){
header('Location: adminpage.php');
}else{
header('Location: homepage.php');
}
}
}
答案 0 :(得分:1)
$userpass
变量包含提交密码的哈希值,因此将其与“admin123”进行比较将无效。
您应该将其与“admin123”的哈希值进行比较,或者不再将它们进行比较,因为您之前已经在代码中执行了此操作。