我正在尝试为普通用户和管理员用户创建登录系统。我在数据库中创建了“ user_rang”列,普通用户的值是-0,管理员-1。我想检查普通用户或管理员在登录时是否登录。如果是admin,则将其移动到adminpanel.php,如果将普通用户移动到index.php。怎么做。现在,我有类似的东西,但是它不起作用。 是login.php:
session_start();
include('config.php');
if(isset($_SESSION['login_id'])){
if (isset($_SESSION['pageStore'])) {
$pageStore = $_SESSION['pageStore'];
header("location: $pageStore");
}
if (isset($_POST['signIn'])) {
if (empty($_POST['email']) || empty($_POST['password'])) {
echo "fill the fields";
}
else
{
$email = $_POST['email'];
$password = $_POST['password'];
$rang = $_POST['user_rang'];
include('config.php');
$sQuery = "SELECT id, password from account where email=? LIMIT 1";
$rQuery = "SELECT user_rang from account where user_rang=";
$stmt = $conn->prepare($sQuery);
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($id, $hash);
$stmt->store_result();
$stmt = $conn->prepare($rQuery);
if ($rang == "1") {
header("location: adminpanel.php");
}
else {
echo('No permission');
}
if($stmt->fetch()) {
if (password_verify($password, $hash)) {
$_SESSION['login_id'] = $id;
if (isset($_SESSION['pageStore'])) {
$pageStore = $_SESSION['pageStore'];
}
else {
$pageStore = "index.php";
}
header("location: $pageStore");
$stmt->close();
$conn->close();
}
else {
echo 'Unable login or password!';
}
} else {
echo 'Unable login or password!';
}
$stmt->close();
$conn->close();
}
}
?>
答案 0 :(得分:0)
您可以通过while循环检查user_range
列:
首先,在查询中添加列:
$sQuery = "SELECT id, password, user_rang from account LIMIT 1";
而不是花一会儿时间user_range
:
while($row = $stmt->fetch_array()){
if ($row['user_rang'] == '1'):
header("location: index.php");
else:
header("location: adminpanel.php");
endif;
}