PHP 多个受限访问


PHP Multiple Restricted Access

我正在尝试在我的 PHP 中使其达到如果帐户数据库值与 0 或 1 或 2 或 3 匹配,那么它会使登录转到某个页面,但到目前为止它没有登录我,也没有带我到页面。在我有一个登录页面之前,但它将其发送到一个普遍受限制的页面,但我想要的是取决于用户注册的内容,然后他会输入这个值(我已经实现了),如果这个页面要工作,那么它会在登录时将他发送到四个受限制的站点之一。我无法获得的是被拉取的值,并用于在登录特定页面时将他发送给他。我正在使用Mysqli。这是代码:

  <?php require 'connections/connections.php'; ?>
   <?php
   if(isset($_POST['Login'])){
        $Username = $_POST['Username'];
        $Password = $_POST['Password'];
        $result = $con->query("select * from user where Username='$Username'
        AND Password='$Password'");
        $row = $result->fetch_array(MYSQLI_BOTH);
        $AccountPerm = $con->query("SELECT * FROM `user` WHERE Account =
         ?");
        session_start();
        $AccountPerm = $_SESSION['Account'];
        if($AccountPerm == 0){
                header("Location: account.php");
        }
        if($AccountPerm == 1){
                header("Location: Account1.php");
        }
        if($AccountPerm == 2){
                header("Location: Account2.php");
        }
        if($AccountPerm == 3){
                header("Location: Account3.php");
        }
}
?>

到目前为止,它还没有让我登录

可以肯定的是,您的帐户.php,帐户1.php,Accout2.php和帐户3.php依赖于$_SESSION['Account']对吗?(下面的代码假设如此)

至于您在登录和重定向方面的问题,您忘记了一行:

$_SESSION['Account'] = $row['Account'];

另外,我删除了

$AccountPerm = $con->query("SELECT * FROM `user` WHERE Account =
     ?");

您的代码应如下所示:

<?php require 'connections/connections.php'; // NOTE: I don't close the php tag here ! See the "session_start()" point in the "Reviews" section below
if(isset($_POST['Login'])){
    $Username = $_POST['Username'];
    $Password = $_POST['Password'];
    // TODO: Sanitize $Username and $Password against SQL injection (More in the "Reviews" section)
    $result = $con->query("select * from user where Username='$Username'
    AND Password='$Password'");
    // TODO: Check if $result return NULL, if so the database couldn't execute your query and you must not continue to execute the code below.
    $row = $result->fetch_array(MYSQLI_BOTH);
    // TODO: Check if $row is NULL, if so the username/password doesn't match any row and you must not execute code below. (You should "logout" the user when user visit login.php, see the "Login pages" point in the "Reviews" section below)
    session_start();
    $_SESSION['Account'] = $row['Account']; // What you forgot to do
    $AccountPerm = $_SESSION['Account'];
    if($AccountPerm == 0){
            header("Location: account.php");
    }
    if($AccountPerm == 1){
            header("Location: Account1.php");
    }
    if($AccountPerm == 2){
            header("Location: Account2.php");
    }
    if($AccountPerm == 3){
            header("Location: Account3.php");
    }
}
?>

评论

  • session_start()
    应该在代码的顶部调用。(它最终可能会出现在一个共享文件中,例如您将包含在所有文件中的connections.php)。一个原因是,如果您在调用session_start()之前将任何字符发送到用户浏览器,session_start()将无法正常工作。

    例如,您在包含 php 标签后关闭 connections.php ,您可能不知道但您的换行符实际上是文本发送到浏览器!

    要解决此问题,您只需不要关闭您的 php 标签,例如在

    <?php require 'connections/connections.php'; ?>
    if(isset($_POST['Login'])){
    
  • 登录页面
    确保在每种情况下都注销(未设置用于检查用户是否登录的变量$_SESSION除非他输入了正确的用户名/密码组合。
    如果用户尝试登录,则可能是与上次不同的用户,如果他的用户名/密码错误,我们不希望他以其他人的身份登录。

  • MySQL检查
  • :在使用MySQL函数之前,您应该始终检查返回给您的内容!(请参阅文档!不这样做会抛出 php 错误/通知。

  • SQL 注入 :在将 $Username/$Password 用于查询之前,必须对其进行清理。

    要么将值追加 $con->real_escape_string(),例如

    $result = $con->query("SELECT * FROM user WHERE Account = '" . $con->real_escape_string($Username) . "' AND Password = '" . $con->real_escape_string($Password) ."')
    

    或者您使用绑定参数,如本文中所述(这是推荐的 WAY

  • 没有多个帐户页面

    您的登录页面应仅重定向到帐户.php并在此页面中根据 $_SESSION['帐户'] 值拆分逻辑。

    没有什么能阻止您包括帐户 1.php、帐户 2.php、...在帐户内.php。
    如果这样做,请将您的帐户 1.php、帐户 2.php、帐户 3.php 放在用户无法浏览的私人文件夹中。
    (其中一种方法是创建一个文件夹(如includes)并放入文件名.htaccess并带有Deny from all