Doing something when the user navigates from the page is the wrong approach because you don’t know if the user will navigate the whole other page (say contact.php for the sake of the argument), or he / she will just go to the next page abc .php and, as Borealid pointed out, you cannot do this without JS. Instead, you can simply add a check and see if there is a user from abc.php:
First, in your abc.php file, a unique variable is set in the $ _SESSION array, which will act as the character that the user had on this page:
$_SESSION['previous'] = basename($_SERVER['PHP_SELF']);
Then add this on all pages before any exit to check if the user is coming from abc.php:
if (isset($_SESSION['previous'])) { if (basename($_SERVER['PHP_SELF']) != $_SESSION['previous']) { session_destroy();
Thus, you will destroy the session (or certain variables) only if the user goes from abc.php and the current page is different.
Hope I could clearly explain this.
source share