user3598554
user3598554

Reputation: 67

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');
?>

Upvotes: 5

Views: 7414

Answers (2)

Dinesh Saini
Dinesh Saini

Reputation: 2916

Modify file logout.php as below:

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

Upvotes: 14

Shiv Aggarwal
Shiv Aggarwal

Reputation: 499

I think your login is also not working because there is no session_start on your login.php file. Start session on your login.php file may it help.

Upvotes: 0

Related Questions