【问题标题】:Prevent php direct page access in the url bar防止url栏中的php直接页面访问
【发布时间】:2013-07-29 17:20:35
【问题描述】:

我在核心 php 中做一个小应用程序。这里我的登录数据库是这样的

id
firstname
lastname
email
userid
account_type
contactno
password

在登录文件中的代码是这样的

<?php
    include_once("include/connection.php");
    session_start();
    session_unset();
?>
<?php
      $msg="";
    if(isset($_REQUEST['sub'])){
        $pswd=sha1($_REQUEST['psd']);
        $sel=mysql_query("select * from login where userid='".$_REQUEST['uid']."' and password='".$pswd."'");

        $rowsel=mysql_num_rows($sel);
        if($rowsel==1){
            $selacc=mysql_fetch_array($sel);
            if($selacc['status']!='banned'){
              $_SESSION['uid']=$selacc['userid'];
              $_SESSION['uname']=$selacc['fname']." ".$selacc['lname'];
              $_SESSION['upassword']=$selacc['password'];
              $_SESSION['acctype']=$selacc['acctype'];
              $_SESSION['agentcode']=$selacc['agent_code'];
              $_SESSION['authentication']="authenticated";
          header("location:dashboard.php");
          }
        }
        else{
          $msg="Enter Valid Username Password";
        }
    }
?>
<body>
    <form name="login-form" method="post" action="#">
    <input type="text" name="uid" class="inputbox" />
    <input type="password" name="psd" class="inputbox" />
    <input type="submit" name="sub" value="" class="inputbotton" />
  </form>

现在登录后,用户被定向为dashboard。但是从这里当我直接输入``一个页面名称(比如说posts.php)时,它会被重定向到post.php文件。但是在这里我想要一个拒绝访问,当有人直接在 url 中输入页面名称(如 post.php)时,它应该显示一些错误。但是当页面正常重定向时,它应该显示页面。我想阻止地址栏中的直接页面访问,但是当页面正常重定向时,它应该显示页面。

【问题讨论】:

  • 与其担心是否有人输入了地址栏(这几乎不可能可靠地检测到),不如在每个页面的开头检查会话以查看用户是否登录?

标签: php session-variables sessionid


【解决方案1】:

例如,只需检查上一页中设置的任何会话变量

 if(!isset($_SESSION['uid'])){
     echo 'error';
     exit;
 }

在上面做

【讨论】:

    【解决方案2】:

    其中有两个因素。

    1) 您在服务器上的文件夹和文件权限。

    2) 当您第一次登录时,它应该显示您的登录页面。但是当你再次做同样的事情时。变量存储在会话中,直到您关闭浏览器。因此,请关闭浏览器并重试。您需要检查是否设置了会话ID,并据此做出决定。

    【讨论】:

      猜你喜欢
      • 2010-11-05
      • 2010-09-16
      • 2016-11-22
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 1970-01-01
      • 2017-11-22
      相关资源
      最近更新 更多