简体   繁体   English

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

[英]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.

相关问题 我如何将我的用户和管理员重定向到其他页面 - How can i redirect my user and admin to different page 我如何将用户重定向到用户页面并将管理员重定向到 cakephp 中的管理页面 - how can i redirect user to the user page and admin to the admin page in cakephp 当非管理员用户在管理面板中登录时,如何隐藏一些导航栏页面? - How can I hide some navbar pages when a non-admin user is logged-in in the admin panel? 登录每个用户的帐户后,如何重定向每个页面。 我希望他们重定向到各自的页面(codeIgniter) - How can I redirect every pages, when I login every user's account. I want them to redirect to their respective pages(codeIgniter) 管理员添加用户后如何重定向管理员? - How to redirect admin after admin add user? 我如何将管理员和员工的页面链接到单独的页面 - how can i link a page for admin and staff with separate pages 如何将新的管理页面添加到YII? - How can I add new admin pages to YII? 我该如何处理在wordpress的管理端添加的页面? - How can i manimulate the pages added in admin side of wordpress? 如何将wp-admin重定向到另一个URL - How can I redirect wp-admin to another url 如何在Wordpress管理页面中设置PHP标头重定向 - How can I set a php header redirect in a wordpress admin page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM