从站点注销

时间:2014-10-14 13:39:32

标签: javascript php html

我使用我的项目php oop。此类从数据库获取详细信息并为用户创建会话。

<?php

 include("../Controller/DBConnection/DBConnectionController.php");
 session_start();

  class LoginModel {

public function getLogin() {
    $name = "";
    $type = "";

    if (isset($_POST["submit"]) && $_POST["submit"] == "submit") {

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

        $query = "SELECT username, password,'guser' AS type FROM g_user WHERE username = '$username' AND password = '$password' UNION
                    SELECT username, password,'suser' AS type FROM s_user WHERE username = '$username' AND password = '$password' UNION
                    SELECT username, password,'admin' AS type FROM admin WHERE username = '$username' AND password = '$password'";

        $result = mysql_query($query); 

        while ($row = mysql_fetch_array($result)) {
            $name = $row["0"];
            $type = $row["type"];
        }

        if (mysql_affected_rows() == 0) {
           return ("invalidlogin");
          // $_SESSION["Error"]=$username;   
        } else {
            $_SESSION["username"] = $name;
           // $_SESSION['userid']   = "";
            if ($type == "admin") {
                $_SESSION["adminname"] = $name;
                return ("adminlogin");

            }else if($type == "guser"){
                return ("guserlogin");

            }else if($type == "suser"){
                return ("suserlogin");

            }
        }
    }
}

}

我想在点击退出链接时退出我的网页。但是当我点击它时,再次加载登录页面,但我点击浏览器后退按钮或查看另一个打开标签我再次加载以前的用户页面。我怎么能单独这个,当我登录网站时我创建以下会话。

这是我的视图类部分代码.witch show log out link。

                    <ul>
                        <li><a href="SUserHome.php">Home</a></li>
                        <li><a href="send D.php">Disease Details</a></li>
                        <li><a href="../../forum/forum_index.php" target="_blank">Forum</a></li>
                        <li><a href="#">History</a></li>
                        <li><a href="#">Contact</a></li>
                        <li><a href="../../Controller/logout.php" >Log Out</a></li>
                    </ul>       

这是logout.php

<?php

  if (session_id())
      session_destroy();

      header("location:../View/LoginView.php");

但没有奏效。 拜托,我希望有一些解决方案。帮助我。

4 个答案:

答案 0 :(得分:1)

您应该检查是否存在会话变量以授予用户访问权限。要从您的网站注销,只需销毁会话,这将阻止有效访问&#39;记录&#39;用户出局:

session_start()
session_destroy(); //destroy sessions but session data will still be avail on same page so redirect is needed after this
header('location:index.php'); // redirect to login or index page or logout success page
exit;

OR

session_start();
unset($_SESSION); //will destroy session superglobal on current and subsequent pages
echo $_SESSION['adminuser']; //will echo nothing at this point 
header('location:index.php'); // redirect to login or index page or logout success page if you want
exit;

答案 1 :(得分:1)

您的session_unset()正在立即执行。将它放在链接中的URL附近并不是在单击链接时执行它。

答案 2 :(得分:0)

使用以下命令创建一个简单的logout.php:

<?php

if(session_id())
    session_destroy();

答案 3 :(得分:0)

创建一个新文件logut.php并将此代码放入其中

session_destroy();
unset($_SESSION['adminname']);
header('location:login.php');