使某种登录模式转到另一个页面位置


Making a certain log in pattern go to another page location

我正在构建一个社交网站。正确注册和登录的用户将被重定向到home.php,并相应地进行$_SESSION

但是我已经手动创建了一个用户名为freddy的管理员用户(登录需要用户名)。我想说的是"如果用户名等于弗雷迪,那就带他去admin_home.php"。

我尝试的是创建两个单独的$_SESSION

为普通用户创建$_SESSION

    // if the user credentials are correct, log the user in:
        $_SESSION["user_login"] = $user_login;
            header( "Location: home.php" ); // refresh page
        exit;
    }   

为管理员创建$_SESSION

    if ($account_type == "admin"){
        // Create seperate session for admin
        $_SESSION["user_login"] = $admin_login;
                header( "Location: admin_home.php" ); // refresh page
            exit;
        }   

完整查询:

<?php
$user_query = mysqli_query ($connect, "SELECT * FROM users WHERE username = '$user_login' AND password = '$decrypted_password' AND closed='no' LIMIT 1");   
    $check_for_user = mysqli_num_rows ($user_query); // checking to see if there is infact a user which those credentials in the DB
        if ($check_for_user==1){
            while ($row = mysqli_fetch_array($user_query)){
                $user_id = $row['id'];
                $account_type = $row['account_type'];
            }
            // if the user credentials are correct, log the user in:
            $_SESSION["user_login"] = $user_login;
                header( "Location: home.php" ); // refresh page
            exit;
        }   
        if ($account_type == "admin"){
            // Create seperate session for admin
            $_SESSION["user_login"] = $admin_login;
                    header( "Location: admin_home.php" ); // refresh page
                exit;
            }           
        else {
                // if user row does not equal 1 ...
                echo "<div class='wrong_login'>
                        <p> Username or password is incorrect, please try again. </p>
                     </div>";       
            exit(); 
        }
}
?>  
使用

当前代码,使用用户名登录 freddy - 这应该带我去admin_home.php ,带我去home.php这不是我想要的。

首先是一个快速建议。如果您希望用户信任您,则不应存储纯文本密码。请参阅此文档有关哈希密码的信息,尤其是有关加盐哈希的部分。

我会说最佳实践是创建一个用户类,匹配您的数据库表,并在用户登录时创建一个实例,并将该类实例存储在您的会话变量中。目前,您不存储用户 ID 或帐户类型等内容,稍后可能需要使用。

正如@FirstOne指出的那样,编写代码的问题在于,一旦用户正确登录,您就会退出,而不是先检查他们的帐户类型。