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');
?>
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.
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.
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.
Modify file logout.php as below:
<?php
session_start();
session_destroy();
unset($_SESSION);
session_regenerate_id(true);
header('LOCATION: login.php');
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With