Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将管理员和用户重定向到他们的页面?_Php_Html_Authentication_Redirect - Fatal编程技术网

Php 如何将管理员和用户重定向到他们的页面?

Php 如何将管理员和用户重定向到他们的页面?,php,html,authentication,redirect,Php,Html,Authentication,Redirect,我无法将用户重定向到他们的页面。我尝试在代码的开头添加类似的内容,但根本不起作用 session_start(); if(isset($_SESSION["loggedin"], $_SESSION["user_type"]) && $_SESSION["loggedin"] === true){ $_SESSION["user_type"] = "admin";

我无法将用户重定向到他们的页面。我尝试在代码的开头添加类似的内容,但根本不起作用

session_start();
 
if(isset($_SESSION["loggedin"], $_SESSION["user_type"]) && $_SESSION["loggedin"] === true){
    $_SESSION["user_type"] = "admin";
    header("location: crud_form/index.php");
    exit;
}
代码的其余部分看起来像这样,我不知道我是否也应该在那里做一些更改

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);
}
如果有人至少告诉我问题出在哪里,我做错了什么,我会非常高兴。 谢谢

在这里,我从数据库中获取了用户类型,并与 第二件事

session_start();

if($_SESSION["user_type"] !== "admin"){     
    header("location: crud_form/index.php"); 
}
 

在哪里保存用户类型?它是否在数据库中?是的,它与用户类型下的用户在同一个表中,可以是用户或管理员
session_start();

if($_SESSION["user_type"] !== "admin"){     
    header("location: crud_form/index.php"); 
}