销毁用户登录会话/ NULL $ _SESSION remnant

时间:2015-06-06 04:16:49

标签: php session login

我正在尝试创建一个用户登录系统,以便在我正在构建的网站上使用。我有登录脚本和注册脚本,但我在注销和销毁会话时遇到问题。

这是我的索引代码。它在config中获取数据库信息(对它没有任何作用),然后运行check-login以确保用户实际登录。它有一个注销按钮,路由到logout.php

<?php 
    include_once("config.php");
    include_once("check-login.php");
    session_start();
    $username = $_SESSION["username"];
?>

<html>
<body>
    <h1>
        Hello <? echo $username ?>! We're still building, but feel free to... wait?
    </h1>
    <form action="logout.php">
        <input class="logoutbutton" type="submit" value="Logout" />
    </form>
</body>
</html>

这是我的check-login.php文件。请注意,无论何时我链接回索引,我都会使用$_GET将一些信息发布到地址栏中。我没有地方可以回到index.php

<?php
    ob_start();
    include_once("../myreadingplanner_config/config.php");

    if(($_SESSION['username']) != null){ //If user is already logged in...
        $username=$_SESSION['username'];
        header("Location: index.php?Message=AlreadyLoggedIn$username");
    } 
    else {

        if(isset($_POST['username']) && strlen($_POST['username'])!=0){ //if username is valid
            $username = $_POST['username'];
        } else {
            header('Location: login.php');
        }

        if(isset($_POST['password']) && strlen($_POST['password'])!=0){
            $password = $_POST['password'];
        } else {
            header('Location: login.php');
        }

        $SQLString = "SELECT TOP(1) * FROM Users WHERE Username = '$username' AND Password = '$password'";
        $result = sqlsrv_query($conn, $SQLString) or die ("");

        if($result != null)
        {   
            $_SESSION['username'] = $username;
            header("Location: index.php?Message=YouLoggedIn$username");
        } else {
            header("Location: index.php?Message=UserLoginNotFound&Username=$username");
    }
}
ob_flush();
?>

最后这是我的logout.php,它应该(理论上)销毁会话,然后返回index.php。当它返回index.php时,index.php将使用include_once("check-login.php");重新路由到login.php

<?php
    session_start();
    session_destroy();
    header('Location: index.php');
?>

只要看看我的逻辑,检查登录就应该是一个无限循环,对吧?因为如果用户登录,它应该重新路由到索引,其中包括check-login,它重新路由到索引,等等......

如果您想亲自查看网站,请访问www.myreadingplanner.com,并使用此信息登录(用户最终将被删除)

Username: StackUser
Password: password1

所以功能明智,除非你有一个有效的会话,否则login.php永远不应该是可见的,当它有,它应该说'Welcome $ username!'。但是如果你点击索引上的注销按钮,它仍会保持会话打开,但它将为空。

关于为什么注销似乎没有完全注销用户的任何建议或者为什么它将用户注销但是保持NULL $ _SESSION?

2 个答案:

答案 0 :(得分:2)

首先,看一下index.php文件。在该文件中,更改以下代码:

include_once("config.php");
include_once("check-login.php");
session_start(); // move the session_start function and place at the top of the script
$username = $_SESSION["username"];

更改它,以便它变成这样:

session_start();
include_once("config.php");
include_once("check-login.php");
$username = $_SESSION["username"];

发生此问题是因为在文件check-login.php中您没有声明函数session_start();

我测试了这个问题。它有效!

答案 1 :(得分:2)

要删除会话,请使用

unset($_SESSION['SESSION_VAR'] );
session_destroy(); //closes the session and prevents session riding

有关详细信息,请参加我的研究会议,因为您应该尽快关闭会话,以防止这种情况发生。

也不要取消整个会话全局数组的设置。

//don't do this
unset($_SESSION);
相关问题