Session_destroy () is not working

My session_destroy () is not working. When I log out as a user and I return to my browser, show the user online. Where is my error? Thank you. :-)

login.php file

<?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;
}
?>

logout.php file

<?php 
    session_start();
    session_destroy();
    header('LOCATION: login.php');
?>
+4
source share
2 answers

Modify the logout.php file as shown below:

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

I think your login also does not work, because there is no session_start in your login.php file. Starting a session in the login.php file may help.

0
source

Source: https://habr.com/ru/post/1539097/


All Articles