1 回答

TA貢獻1804條經驗 獲得超3個贊
您的代碼錯誤,您的代碼無法設置會話,當條件成立時,它會根據您使用的條件將用戶重定向到另一個 .php 頁面。
試試這個代碼。
<?php session_start(); ?>
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$row = mysqli_fetch_assoc($results);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
//if you want to redirect user as per its type, you can use this condition
if('student' == $row['user_type']) {
header('Location: ogrenciarayuzu.php');
exit();
} elseif ('department' == $row['user_type']) {
header('Location: bolumsekreterligiarayuzu.php');
exit();
} elseif ('institute' == $row['user_type']) {
header('Location: enstituarayuzu.php');
exit();
}
else
{
header('location: index.php');
}
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
- 1 回答
- 0 關注
- 140 瀏覽
添加回答
舉報