session_destroy()不起作用

时间:2014-05-03 08:34:57

标签: php session logout

我的session_destroy()不起作用。当我以用户身份注销时,我返回浏览器,在线显示用户。哪里有错误?谢谢。 : - )

file login.php

<?php
require_once('functions.php');
if(isset($_POST['submit'])){
    if((isset($_POST['username']) && $_POST['username'] != '') && (isset($_POST['password']) && $_POST['password'] != '')){
        $connection = conectDatabase();
        $query = "SELECT * FROM users WHERE username='" . $_POST['username'] . "' AND password='" . $_POST['password'] . "';";
        $result = $connection->query($query);
        if ($result->num_rows >0){
             session_start(); 
             $_SESSION['username'] = $_POST['username'];
             header('LOCATION: users.php');  
        } else {
             echo '<p class="formMessage">Username/password do not match !</p>';
        }
    } else {
        echo '<p class="formMessage">Username/Password field is empty</p>';
    }
}                        
?>

file functions.php

<?php
function conectDatabase() {
    $connection = new mysqli('localhost', 'root', '', 'DB');     

    if(mysqli_connect_errno()) {
        die("error DB: " . mysqli_error() . "(" . mysqli_connect_errno() . ").");
    }
    return $connection;
}
?>

file logout.php

<?php 
    session_start();
    session_destroy();
    header('LOCATION: login.php');
?>

2 个答案:

答案 0 :(得分:13)

修改文件logout.php,如下所示:

<?php 
    session_start();
    session_destroy();
    unset($_SESSION);
    session_regenerate_id(true);
    header('LOCATION: login.php');
?>

答案 1 :(得分:0)

我认为您的登录也无法正常工作,因为您的login.php文件中没有session_start。在login.php文件上启动会话可能会有所帮助。