Jump to content

unsetting sessions vars


ksmatthews

Recommended Posts

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

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
}
?>

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.