[英]How can I redirect admin and user to their pages?
I have a problem with redirecting users to their pages.我在将用户重定向到他们的页面时遇到问题。 I tried adding something like this at the beginning of the code but it does not work at all.我尝试在代码的开头添加类似这样的内容,但它根本不起作用。
session_start();
if(isset($_SESSION["loggedin"], $_SESSION["user_type"]) && $_SESSION["loggedin"] === true){
$_SESSION["user_type"] = "admin";
header("location: crud_form/index.php");
exit;
}
The rest of the code looks like this and I dont know if maybe I should do some changes there also?代码的 rest 看起来像这样,我不知道我是否也应该在那里做一些更改?
require_once "config.php";
$username = $password = "";
$username_err = $password_err = "";
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty(trim($_POST["username"]))){
$username_err = "Wprowadź login.";
} else {
$username = trim($_POST["username"]);
}
if(empty(trim($_POST["password"]))){
$password_err = "Wprowadź hasło.";
} else {
$password = trim($_POST["password"]);
}
if(empty($username_err) && empty($password_err)){
$sql = "SELECT user_id, username, password FROM users WHERE username = ?";
if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt, "s", $param_username);
$param_username = $username;
if(mysqli_stmt_execute($stmt)){
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_rows($stmt) == 1){
mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password);
if(mysqli_stmt_fetch($stmt)){
if(password_verify($password, $hashed_password)){
session_start();
$_SESSION["loggedin"] = true;
$_SESSION["user_id"] = $id;
$_SESSION["username"] = $username;
header("location: crud_form/index.php");
} else {
$password_err = "Hasło nieprawidłowe.";
}
}
} else {
$username_err = "Nie ma takiego użytkownika.";
}
} else {
echo "Coś się zepsuło! Spróbuj ponownie.";
}
mysqli_stmt_close($stmt);
}
}
mysqli_close($link);
}
I would be super happy if someone would tell me at lest where is the problem and what am I doing wrong.如果有人能告诉我至少问题出在哪里以及我做错了什么,我会非常高兴。 Thanks!谢谢!
if(empty($username_err) && empty($password_err)){
$sql = "SELECT user_id, username, password, user_type FROM users WHERE username = ?";
if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt, "s", $param_username);
$param_username = $username;
if(mysqli_stmt_execute($stmt)){
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_rows($stmt) == 1){
mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password, $user_type);
if(mysqli_stmt_fetch($stmt)){
if(password_verify($password, $hashed_password)){
session_start();
$_SESSION["loggedin"] = true;
$_SESSION["user_id"] = $id;
$_SESSION["username"] = $username;
$_SESSION["user_type"] = $user_type;
Here i've fetched user_type from database, and made a session with Second thing在这里,我从数据库中获取了 user_type,并使用 Second thing 制作了 session
session_start();
if($_SESSION["user_type"] !== "admin"){
header("location: crud_form/index.php");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.