Destroying a session PHP

Designing Solutions Through Programming. This course is also called introduction to programming.
Forum rules
Please be careful about sharing personal information. Never use any identifiable information.
Post Reply
User avatar
julia
New User
Posts: 3
Joined: 23 Aug 2018, 11:50

Destroying a session PHP

Post by julia » 11 Apr 2019, 19:54

I want to unset all variables of the session when a user goes back to the homepage. I have the session_destroy() function but the variables still stay in the array.
Screen Shot 2019-04-11 at 19.49.22.png
Screen Shot 2019-04-11 at 19.49.22.png (19.63 KiB) Viewed 66 times
Screen Shot 2019-04-11 at 19.48.30.png
Screen Shot 2019-04-11 at 19.48.30.png (18.61 KiB) Viewed 66 times
1 x

User avatar
Mr. MacKenty
Site Admin
Posts: 119
Joined: 28 Apr 2018, 17:06
Answers: 3

Re: Destroying a session PHP

Post by Mr. MacKenty » 08 May 2019, 08:56

Please use this:

Code: Select all


<?php
// This file logs out a user.
// in order to destroy a session we must first open one!
// this page will redirect a user to index.php after loggin out. 
// This file should be named logout.php
session_start();
if (isset($_SESSION['unique_id_of_logged_in_user'])) {
    $unique_id_of_logged_in_user = $_SESSION['unique_id_of_logged_in_user'];
    include('database_inc.php');
    $logout_query = mysqli_query($connect,"UPDATE users SET logged_in_now = 0 WHERE id = '$unique_id_of_logged_in_user';");
    // the code from 2 lines below is used from https://stackoverflow.com/questions/3989347/php-why-cant-i-get-rid-of-this-session-id-cookie
    $params = session_get_cookie_params();
    setcookie(session_name(), '', 0, $params['path'], $params['domain'], $params['secure'], isset($params['httponly']));
    session_unset();
    session_destroy();
}
header('location:index.php');
?>
0 x

Post Reply