AdRock Posted April 4, 2010 Share Posted April 4, 2010 This is probably really easy but what i want to do is unset a session when the user changes page. This session should only be used on a certain page. would it be something like if($_SERVER['PHP_SELF']) !="mypage.php") unset $_SESSION['thissession']; Quote Link to comment https://forums.phpfreaks.com/topic/197562-unset-session-on-leaving-a-certain-page/ Share on other sites More sharing options...
the182guy Posted April 4, 2010 Share Posted April 4, 2010 This is probably really easy but what i want to do is unset a session when the user changes page. This session should only be used on a certain page. would it be something like if($_SERVER['PHP_SELF']) !="mypage.php") unset $_SESSION['thissession']; Use unset($_SESSION['somevariable']) to delete a specific session variable. If you want to delete the entire session then use session_destroy() which will kill the session completely. Or, you could just not include session_start() on the pages that don't need the session data. Quote Link to comment https://forums.phpfreaks.com/topic/197562-unset-session-on-leaving-a-certain-page/#findComment-1036850 Share on other sites More sharing options...
ignace Posted April 4, 2010 Share Posted April 4, 2010 On the page you want to use sessions create the session and add the CREATE_URI (=$_SERVER['REQUEST_URI']) variable then on the other pages add this code: if (isset($_SESSION['CREATE_URI']) && $_SESSION['CREATE_URI'] !== $_SERVER['REQUEST_URI']) { unset($_SESSION['CREATE_URI']); } Quote Link to comment https://forums.phpfreaks.com/topic/197562-unset-session-on-leaving-a-certain-page/#findComment-1036861 Share on other sites More sharing options...
AdRock Posted April 4, 2010 Author Share Posted April 4, 2010 Thanks chaps....works perfectly Quote Link to comment https://forums.phpfreaks.com/topic/197562-unset-session-on-leaving-a-certain-page/#findComment-1036864 Share on other sites More sharing options...
AdRock Posted April 5, 2010 Author Share Posted April 5, 2010 Right I've been playing around and this only works on the same url such as mypage.php?thisvar=1&thatvar=2 If i add another var to the end of the url, the session gets unset becuase the url isn't exactly the same. What this is for is filtering results so i can choose how many results to display and the order wether it's asc or desc and I need to use paging which all works except the url changing from mypage.php?thisvar=1&thatvar=2 to mypage.php?thisvar=1&thatvar=2&pagenum=2 unsets the session Quote Link to comment https://forums.phpfreaks.com/topic/197562-unset-session-on-leaving-a-certain-page/#findComment-1036941 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.