PHP会话......(必须销毁)

时间:2014-03-16 19:21:25

标签: php session destroy

我想为我的网站创建一个PHP会话......

我想要的是,一旦任何人开始会话,它只能看到/访问网站的特定页面......当他/她点击其他链接时(我不想让他访问),会话必须破坏......

他必须在下次开始会话时输入用户名/密码......

    $con=mysql_connect("localhost","root","");
    mysql_select_db("school", $con);

    $un=$_POST["username"];
    $pass=$_POST["password"];

    $query= "SELECT username FROM login where username= '".$un."' AND  password = '".$pass."' ";
    $result = mysql_query($query)or die(mysql_error());
    $data = mysql_fetch_array($result);
    $rows = mysql_num_rows($result);
    if($rows > 0)
    {
        session_start();
        $_SESSION['username']=$data['username'];
        header("location: ../Index3.html");
    }
    else
    {
        header("location: ../Pages/Admin_panel.html");
        ?>
        <script>
        alert("Unable to Sign In!");
        </script>
        <?php
    }

提前致谢!

1 个答案:

答案 0 :(得分:1)

首先,那里:

  header("location: ../Pages/Admin_panel.html");
  ?>
  <script>
  alert("Unable to Sign In!");
  </script>
  <?php

header()搬迁后的所有内容都不会被执行。

相反,您可以使用:

else
{
    ?>
    <script>
    alert("Unable to Sign In!");
    window.location.replace('/Pages/Admin_panel.html')
    </script>
    <?php die();
}

然后在您需要权限的其他页面中,添加以下代码:

<?php

if($_SESSION['user_rights'] != 'admin'){ //whatever rights he would need to access this page
    session_destroy();
    header("location: ../Pages/login.html"); //your login page.
}
?>