Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

End Session in PHP [duplicate]

Tags:

php

i have problem in End session, i have MAIN.PHP where my session starts, inside my MAIN.php i have<a href = "logout.php">logout</a> this is the code for my logout.php

<?php
 unset($_SESSION['user']);
 unset($_SESSION['pass']);
 session_destroy();
 echo"<script> window.location.href = '../index.php' ; </script>";
 exit();
 ?>

after clicking the link logout, when i press the back button i can still access the pages and still it holds my SESSION variables.

like image 731
Jaycee Avatar asked Sep 03 '26 20:09

Jaycee


1 Answers

I'm going to take a wild stab and say that this answer will solve your problem.

Per the PHP Docs For session_destroy():

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.

In order to kill the session altogether, like to log the user out, the session id must also be unset. If a cookie is used to propagate the session id (default behavior), then the session cookie must be deleted. setcookie() may be used for that.

To sum up what seems to be your problem, the pesky session cookie (which identifies the session) is not being unset. Therefore, when you hit the back button, and session_start(); appears at the top of your script, the global session variables are still being referenced. As the other answer suggests, try:

$_SESSION = array();

to clear all of the global session data. Also, as the docs say, unset the session cookie.

like image 156
War10ck Avatar answered Sep 05 '26 12:09

War10ck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!