Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

session_destroy() not working

My session_destroy () does not work. When I logout as a user, and I go back on my browser, show user online. Where I have the error? Thanks. :-)

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');
?>
like image 285
user3598554 Avatar asked May 03 '14 08:05

user3598554


People also ask

What is PHP session_start () and Session_destroy () function?

session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called. Note: You do not have to call session_destroy() from usual code.

How do you destroy a specific session?

Destroying a PHP Session A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.

How do you check if PHP session is set?

You can check whether a variable has been set in a user's session using the function isset(), as you would a normal variable. Because the $_SESSION superglobal is only initialised once session_start() has been called, you need to call session_start() before using isset() on a session variable.


1 Answers

Modify file logout.php as below:

<?php 
    session_start();
    session_destroy();
    unset($_SESSION);
    session_regenerate_id(true);
    header('LOCATION: login.php');
?>
like image 176
Dinesh Saini Avatar answered Sep 18 '22 11:09

Dinesh Saini