繁体   English   中英

PHP多重限制访问

[英]PHP Multiple Restricted Access

我正在我的PHP中尝试将其设置为如果Account数据库值与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");
        }
}

?>

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

可以肯定的是,您的Account.php,Account1.php,Accout2.php和Account3.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()将不起作用。

    例如,您在包含connections.php之后关闭了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) ."') 

    或者您使用bind参数,如本博文所述这是推荐方法

  • 没有多个帐户页面

    您的登录页面应仅重定向到accout.php,并在该页面内根据$ _SESSION ['Account']值拆分逻辑。

    没有什么可以阻止您在account.php中包含account1.php,account2.php...。
    如果这样做,请将您的account1.php,account2.php,account3.php 放在用户无法浏览的专用文件夹中。
    (方法之一是创建一个文件夹(例如includes ),并在其中放一个Deny from all带有Deny from all的文件名.htaccess

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM