我有一个包含以下字段的 mysql 表:
编号 用户名 密码 作用
管理员的角色编号为 1,普通用户的角色编号为 2
<?php
// we must never forget to start the session
session_start();
$errorMessage = '';
if (isset($_POST['username']) && isset($_POST['password'])) {
include 'library/connect.php';
$username = $_POST['username'];
$password = $_POST['password'];
// check if the user id and password combination exist in database
$sql = "SELECT role FROM user WHERE username = '$username' AND password = '$password'";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
$row = mysql_fetch_array($result);
if (mysql_num_rows($result) == 1 AND $row['role']==2) {
// the user id and password match,
// set the session
$_SESSION['userame'] = true;
// after login we move to the main page
header('Location: login_success.php');
exit;
}
elseif (mysql_num_rows($result) == 1 AND $row['role']== 1) {
// the user id and password match,
// set the session
$_SESSION['admin'] = true;
$_SESSION['username'] = true;
// after login we move to the main page
header('Location: admin.php');
exit;
}
else {
$errorMessage = 'Sorry, wrong user id / password <a href="login.html">Go Back</a>';
}
include 'library/closedb.php';
}
?>
<?php
if ($errorMessage != '') {
?>
<p align="center"><strong><font color="#990000"><?php echo $errorMessage; ?></font></strong></p>
<?php
}
?>
当管理员登录时,它工作正常并被重定向到 admin.php 而当普通用户登录时,什么也没有发生,页面只是被刷新。我做错了什么?
最佳答案
您在 role=2
的第一个 if
block 中拼错了用户名
$_SESSION['用户名'] = true;
关于php登录不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8175332/