Reputation: 1175
How can I unset SESSION['variables']
when user is leaving a page, without having to include something on the top of every single page which I have on my site?
Any best practises around you might know of?
Thanks.
EDIT
I have a wizard for setting up a company and a customer.This includes 2 pages: 1 for company and 1 for customer. If the user wants to re-use the data filled in at the company for the customer, he can check the checkbox and when form at page 1 (company) is posted, at page 2 (customer) those fields are automatically set (think about fields like: postal adress, street, phone etc.). The user can access page 1 and page 2 whenever he wants, so I have to unset the session else it will keep auto-filling in the fields.
I can unset the session after the fields are set on page 2, but when he refreshes the browser, the fields become empty again. This is not a big problem ofcourse, but isn't there an easy way to only unset these sessions when the user goes to any other page (without exiting browser and within my website)?
EDIT
My code:
Company page:
$_SESSION['wizard2']= "true"; // used for manipulating page when entering in wizard mode
if (isset($_POST['savedata'])) // if user wants to re-use data from page1
{
$_SESSION['savedata'] = "true";
$_SESSION['custprovincesession']=mysql_real_escape_string($_POST['compprovince']);
header("Location:customer_page.php");
exit();
Customer page:
if (isset($_SESSION['wizard2'])) // used for manipulating page when entering in wizard
{
unset ($_SESSION['wizard2']);
}
if (isset($_SESSION['savedata'])) // if user wants to re-use data from page1
{
$_SESSION['wizard3'] = "true";
}
<tr>
<th><label for="custprovince">Province:</label></th>
<th><input type="text" name="custprovince" style=width:200px id="custprovince" size="8" value="<?php if (isset($_SESSION['wizard3'])) { echo $_SESSION['custprovincesession'];}?>"/></th>
<th> </th>
<th> </th>
</tr>
at the end of page 2:
if (isset($_SESSION['wizard3']))
{
unset ($_SESSION['wizard3']);
unset ($_SESSION['custprovincesession']);
}
Because of this, by refreshing the page the sessions are unset. Is there no other way to unset these sessions when the user leaves the "customer_page.php"?
Upvotes: 3
Views: 7281
Reputation: 79
you can do this in two ways.
1) session_destroy(); by using this function.
2) unset($_session[$var_name or 'session_name']);
Upvotes: 1
Reputation: 1239
If I understand the question correctly, then I would actually not use the session, and rely on POST variables between the pages.
What I believe you want to do is ONLY keep certain form fields when you go directly from the company page into the customer page (or possibly the other way around?). When the user visits the customer page directly, or later on, the fields need to be clear.
In that case, you simply make sure that when submitting the form, you post to the other page and collect the data from the REQUEST.
If you are actually POST'ing the form elsewhere (to process the addition of the company), and then forwarding on to the form page for the customer, then you can either post a hidden form, or you could re-arrange your workflow so that the insert of company and customer are done at the end.
Upvotes: 2
Reputation: 1239
This might be a silly question, but why would you want to unset certain session variables on every page?
Surely the point of the session is to persist across page views, and thus you would only want to unset or clear it at specific trigger points (such as a user logging out of the system.
To answer the question, many frameworks would generally use a single entry point for all scripts, which does any initialisation, farms off the actual page work to a controller, and then return to the single entry point. If you use this methodology then you can put any closing logic at the end of the page.
Upvotes: 3
Reputation: 174957
A session is automatically destroyed when the connection dies. If a user closed his browser window, or all of the tabs with your pages, the session would be destroyed by itself.
Upvotes: 0