按会话检查页面权限?

时间:2016-06-30 08:00:24

标签: php html security session

我将header.php包含在SESSION的标题徽标和检查页面权限的每个页面中!

  • 因此,如果您在没有登录(会话空状态)的情况下请求 main.php ,它将显示登录页面并在header.php中退出,以显示当前页面内容。
  • header.php 适用于login.php以外的每个页面。因为登录页面不是显示登录表单

所以我想显示登录表单,如何在header.php中查看?抱歉我的英语不好:(

的login.php

<?php
session_start();
include("header.php");
if($_POST){
 //set session logged in
}
?>
<div class="login-box ">
            <h3> Log In </h3>
            <form method="POST">
                <input type="text" name="user"  placeholder="Type User Name"><br>
                <input type="password" name="pass"  placeholder="Type Password"><br>
                <input type="submit" name="submit" value="Login" class="button">
            </form>
            <span id="signup_text">You are not still a member.Click <a href="signup.php">Sign Up</a></span>
</div>

的header.php

<?php
session_start();
?>
<div id="header" class="container">
        <div id="logo">
            <h1>Online Quiz Management</h1>
        </div> 
</div>
<?php        
 if (isset($_SESSION['login'])) {
            echo "<div id='menu'><ul><li><a href=\"sublist.php\">HOme</a></li><li><a href=\"signout.php\">Signout</a></li></ul></div>";
           }
        else {
             echo "<div class=head1> Your are not logged in<br> Please <a href=login.php>Login</a><div>";
             exit;
        }
            ?>
        </div>
    </div>                  
?>    

main.php

<?php
session_start();
include("header.php");
?>
//show main code  if logged in 

2 个答案:

答案 0 :(得分:0)

我认为你不需要将header.php包含在login.php中,因为它检查会话,而login.php是会话启动器...

毕竟,尝试将其添加到header.php:

session_start();
if(!isset($_SESSION['login'])){
echo  '<script language="javascript">';
echo  'alert("Please Login")';
echo  '</script>';
echo("<script>location.href = 'login.php';</script>");//direct the user to login.php if they aren't logged in
}

答案 1 :(得分:0)

只需添加一个检查,如果当前页面是login.php,如果是

,请不要运行您的块
  else if(!basename($_SERVER['PHP_SELF']) == "login.php") {

             echo "<div class=head1> Your are not logged in<br> Please <a href=login.php>Login</a><div>";
             exit;
        }