PHP多重限制访问

时间:2016-01-24 01:12:39

标签: php mysqli

我正在尝试使用我的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");
        }
}

?>

1 个答案:

答案 0 :(得分:0)

  

到目前为止它没有登录我

为了确保您的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&gt; real_escape_string()附加值,例如

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

    或您使用bind parameter, such as explained in this post这是建议的方式

  • 没有多个帐户页面

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

    没有什么可以阻止你在account.php中包含account1.php,account2.php,.... 如果您这样做,请将您的account1.php,account2.php,account3.php in a private folder设置为用户无法浏览。
    (其中一种方法是创建一个文件夹(例如includes)并将文件名.htaccessDeny from all放在一起)