ksmatthews Posted November 12, 2008 Share Posted November 12, 2008 HI There, My display pages that display table data use session vars that 'remember' SQL queries for example when that page is reloaded When I click on another display page these previous page specific session vars need to be unset before the new display page is displayed. I want to do this WITHOUT adding unset(<session cookie>) at the beginning of every display page. I only need to unset these session vars ONCE - when the page is FIRST loaded, NOT every time . Is it possible to UNSET session vars as one goes from one page to another ?? regards, Steven M Link to comment https://forums.phpfreaks.com/topic/132415-unsetting-sessions-vars/ Share on other sites More sharing options...
redarrow Posted November 12, 2008 Share Posted November 12, 2008 look up session_destroy() Link to comment https://forums.phpfreaks.com/topic/132415-unsetting-sessions-vars/#findComment-688423 Share on other sites More sharing options...
PFMaBiSmAd Posted November 12, 2008 Share Posted November 12, 2008 Detect when the page changes and clear your variables when a different page is detected - <?php session_start(); $_SESSION['last_page'] = isset($_SESSION['last_page']) ? $_SESSION['last_page'] : FALSE; // set default value if not set if($_SERVER['REQUEST_URI'] <> $_SESSION['last_page']){ // this is a different page // clear any variables here $_SESSION['last_page'] = $_SERVER['REQUEST_URI']; // set the 'last_page' to be the current page } ?> Link to comment https://forums.phpfreaks.com/topic/132415-unsetting-sessions-vars/#findComment-688446 Share on other sites More sharing options...
ksmatthews Posted November 12, 2008 Author Share Posted November 12, 2008 HI THere, THanks a lot for that. Your code works well .. <?php session_start(); $_SESSION['last_page'] = isset($_SESSION['last_page']) ? $_SESSION['last_page'] : FALSE; // set default value if not set if($_SERVER['REQUEST_URI'] <> $_SESSION['last_page']){ // this is a different page // clear any variables here $_SESSION['last_page'] = $_SERVER['REQUEST_URI']; // set the 'last_page' to be the current page } ?> However if there are vars appended to the URI it might not work as intended For example ... http://localhost/swordfish2/system_admin_display.php?page=1 AND http://localhost/swordfish2/system_admin_display.php?page=2 do not have the same $_SERVER['REQUEST_URI'] even though the base URL is the same. If this code is prepended, it works a s expected .. // trim URI to remove any params, if any, from URI string $URI_array = explode("?", $_SERVER['REQUEST_URI']); $_SERVER['REQUEST_URI'] = $URI_array[0]; Thanks again, Steven M Link to comment https://forums.phpfreaks.com/topic/132415-unsetting-sessions-vars/#findComment-688520 Share on other sites More sharing options...
PFMaBiSmAd Posted November 12, 2008 Share Posted November 12, 2008 Use $_SERVER['SCRIPT_NAME'] instead. I specifically used 'REQUEST_URI' because something like ?page=1, ?page=2 would mean different pages in most cases. Link to comment https://forums.phpfreaks.com/topic/132415-unsetting-sessions-vars/#findComment-688531 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.