FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » how to unset session variable when leaving page
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
how to unset session variable when leaving page [message #174682] Mon, 27 June 2011 18:48 Go to next message
Co is currently offline  Co
Messages: 75
Registered: May 2011
Karma: 0
Member
Hi All,

I use a session variable to search for certain data in my records.
When a submit button is hit a session var gets filled with what I am
looking for.

if(isset($_POST['listByq']) && $_POST['listByq'] != "add") {
unset($_SESSION['listByq']);
unset($_SESSION['valueByq']);
$option = "";
$_SESSION['listByq'] = $_POST['listByq'];
if(isset($_POST['fname'])) { $_SESSION['valueByq'] =
$_POST['fname']; }
if(isset($_POST['country'])) { $_SESSION['valueByq'] =
$_POST['country']; }
}

If I browse to the next page but still using the same search query I
check again:

// IF WE HAVE A SESSION THEN FILL $OPTION WITH A VALUE
if(isset($_SESSION['listByq'])) {
$option = $_SESSION['listByq'];
if ($option == "by_country") {
$country = $_SESSION['valueByq']; //$_POST['country'];

$queryString = "WHERE country='$country' AND email_activated='1'";
$queryMsg = "Showing Members from the country you searched for";

} else if ($option == "by_firstname") {

$firstname = $_SESSION['valueByq']; //
$_POST['fname'];
$firstname = stripslashes($firstname);
$firstname = strip_tags($firstname);
$firstname = eregi_replace("`", "", $firstname);
$firstname = mysql_real_escape_string($firstname);
$queryString = "WHERE rank LIKE '%$firstname%' OR firstname LIKE '%
$firstname%' OR lastname LIKE '%$firstname%' AND email_activated='1'";
$queryMsg = "Showing Members with the name you searched for";

} else if ($option == "newest_members") {

$queryString = "WHERE email_activated='1' ORDER BY id DESC";
$queryMsg = "Showing Newest to Oldest Members";
}

} // end of if(isset($_SESSION....

This is all working like I want but when the user leaves the page and
returns later the session var is still
filled with the last search. I tried to clear the session var at the
beginning of the page but then when I browse
to the next page the session var will also be emptied.

Is it possible to clear the session var when leaving the page?

Marco
Re: how to unset session variable when leaving page [message #174737 is a reply to message #174682] Fri, 01 July 2011 03:41 Go to previous messageGo to next message
Martin is currently offline  Martin
Messages: 8
Registered: October 2010
Karma: 0
Junior Member
On Jun 27, 7:48 pm, Co <vonclausow...@gmail.com> wrote:
> Hi All,
>
> I use a session variable to search for certain data in my records.
> When a submit button is hit a session var gets filled with what I am
> looking for.
>
> if(isset($_POST['listByq']) && $_POST['listByq'] != "add") {
>     unset($_SESSION['listByq']);
>         unset($_SESSION['valueByq']);
>         $option = "";
>         $_SESSION['listByq'] = $_POST['listByq'];
>         if(isset($_POST['fname'])) { $_SESSION['valueByq'] =
> $_POST['fname']; }
>         if(isset($_POST['country'])) { $_SESSION['valueByq'] =
> $_POST['country']; }
>
> }
>
> If I browse to the next page but still using the same search query I
> check again:
>
> // IF WE HAVE A SESSION THEN FILL $OPTION WITH A VALUE
> if(isset($_SESSION['listByq'])) {
>    $option = $_SESSION['listByq'];
>  if ($option == "by_country") {
>         $country = $_SESSION['valueByq'];    //$_POST['country'];
>
>     $queryString = "WHERE country='$country' AND email_activated='1'";
>     $queryMsg = "Showing Members from the country you searched for";
>
> } else if ($option == "by_firstname") {
>
>         $firstname = $_SESSION['valueByq'];                    //
> $_POST['fname'];
>         $firstname = stripslashes($firstname);
>     $firstname = strip_tags($firstname);
>         $firstname = eregi_replace("`", "", $firstname);
>         $firstname = mysql_real_escape_string($firstname);
>     $queryString = "WHERE rank LIKE '%$firstname%' OR firstname LIKE '%
> $firstname%' OR lastname LIKE '%$firstname%' AND email_activated='1'";
>     $queryMsg = "Showing Members with the name you searched for";
>
> } else if ($option == "newest_members") {
>
>     $queryString = "WHERE email_activated='1' ORDER BY id DESC";
>         $queryMsg = "Showing Newest to Oldest Members";
>
> }
> }  // end of if(isset($_SESSION....
>
> This is all working like I want but when the user leaves the page and
> returns later the session var is still
> filled with the last search. I tried to clear the session var at the
> beginning of the page but then when I browse
> to the next page the session var will also be emptied.
>
> Is it possible to clear the session var when leaving the page?
>
> Marco

I've had to do this before and simply added a body onunload event
listener to the page:

<body onunload="clearSession()">

Calls a javascript function:

function clearSession(){
// here make a request to a PHP script called clear_session.php
}

Finally the clear_session.php script:

<?php
unset($_SESSION['myVar'];
?>

Martin.
Re: how to unset session variable when leaving page [message #174738 is a reply to message #174682] Fri, 01 July 2011 09:01 Go to previous messageGo to next message
Martin is currently offline  Martin
Messages: 8
Registered: October 2010
Karma: 0
Junior Member
On Jun 27, 7:48 pm, Co <vonclausow...@gmail.com> wrote:
> Hi All,
>
> I use a session variable to search for certain data in my records.
> When a submit button is hit a session var gets filled with what I am
> looking for.
>
> if(isset($_POST['listByq']) && $_POST['listByq'] != "add") {
>     unset($_SESSION['listByq']);
>         unset($_SESSION['valueByq']);
>         $option = "";
>         $_SESSION['listByq'] = $_POST['listByq'];
>         if(isset($_POST['fname'])) { $_SESSION['valueByq'] =
> $_POST['fname']; }
>         if(isset($_POST['country'])) { $_SESSION['valueByq'] =
> $_POST['country']; }
>
> }
>
> If I browse to the next page but still using the same search query I
> check again:
>
> // IF WE HAVE A SESSION THEN FILL $OPTION WITH A VALUE
> if(isset($_SESSION['listByq'])) {
>    $option = $_SESSION['listByq'];
>  if ($option == "by_country") {
>         $country = $_SESSION['valueByq'];    //$_POST['country'];
>
>     $queryString = "WHERE country='$country' AND email_activated='1'";
>     $queryMsg = "Showing Members from the country you searched for";
>
> } else if ($option == "by_firstname") {
>
>         $firstname = $_SESSION['valueByq'];                    //
> $_POST['fname'];
>         $firstname = stripslashes($firstname);
>     $firstname = strip_tags($firstname);
>         $firstname = eregi_replace("`", "", $firstname);
>         $firstname = mysql_real_escape_string($firstname);
>     $queryString = "WHERE rank LIKE '%$firstname%' OR firstname LIKE '%
> $firstname%' OR lastname LIKE '%$firstname%' AND email_activated='1'";
>     $queryMsg = "Showing Members with the name you searched for";
>
> } else if ($option == "newest_members") {
>
>     $queryString = "WHERE email_activated='1' ORDER BY id DESC";
>         $queryMsg = "Showing Newest to Oldest Members";
>
> }
> }  // end of if(isset($_SESSION....
>
> This is all working like I want but when the user leaves the page and
> returns later the session var is still
> filled with the last search. I tried to clear the session var at the
> beginning of the page but then when I browse
> to the next page the session var will also be emptied.
>
> Is it possible to clear the session var when leaving the page?
>
> Marco

I've had to do this before and achieved it by calling a server-side
script when the web page is unloaded.

In the web page HTML i add an event handler for the body onunload
event:

<body onunload="endSession()">

A javascript function calls the script - use whatever method to call
the script that you are familiar with:

function endSession(){
// here call your server-side script, let's call it end_session.php
}

Finally end_session.php can simply unset your session variable:

<?php
unset $_SESSION['myVar'];
?>
Martin.
Re: how to unset session variable when leaving page [message #174739 is a reply to message #174737] Fri, 01 July 2011 09:36 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 6/30/2011 11:41 PM, Martin wrote:
> On Jun 27, 7:48 pm, Co<vonclausow...@gmail.com> wrote:
>> Hi All,
>>
>> I use a session variable to search for certain data in my records.
>> When a submit button is hit a session var gets filled with what I am
>> looking for.
>>
>> if(isset($_POST['listByq'])&& $_POST['listByq'] != "add") {
>> unset($_SESSION['listByq']);
>> unset($_SESSION['valueByq']);
>> $option = "";
>> $_SESSION['listByq'] = $_POST['listByq'];
>> if(isset($_POST['fname'])) { $_SESSION['valueByq'] =
>> $_POST['fname']; }
>> if(isset($_POST['country'])) { $_SESSION['valueByq'] =
>> $_POST['country']; }
>>
>> }
>>
>> If I browse to the next page but still using the same search query I
>> check again:
>>
>> // IF WE HAVE A SESSION THEN FILL $OPTION WITH A VALUE
>> if(isset($_SESSION['listByq'])) {
>> $option = $_SESSION['listByq'];
>> if ($option == "by_country") {
>> $country = $_SESSION['valueByq']; //$_POST['country'];
>>
>> $queryString = "WHERE country='$country' AND email_activated='1'";
>> $queryMsg = "Showing Members from the country you searched for";
>>
>> } else if ($option == "by_firstname") {
>>
>> $firstname = $_SESSION['valueByq']; //
>> $_POST['fname'];
>> $firstname = stripslashes($firstname);
>> $firstname = strip_tags($firstname);
>> $firstname = eregi_replace("`", "", $firstname);
>> $firstname = mysql_real_escape_string($firstname);
>> $queryString = "WHERE rank LIKE '%$firstname%' OR firstname LIKE '%
>> $firstname%' OR lastname LIKE '%$firstname%' AND email_activated='1'";
>> $queryMsg = "Showing Members with the name you searched for";
>>
>> } else if ($option == "newest_members") {
>>
>> $queryString = "WHERE email_activated='1' ORDER BY id DESC";
>> $queryMsg = "Showing Newest to Oldest Members";
>>
>> }
>> } // end of if(isset($_SESSION....
>>
>> This is all working like I want but when the user leaves the page and
>> returns later the session var is still
>> filled with the last search. I tried to clear the session var at the
>> beginning of the page but then when I browse
>> to the next page the session var will also be emptied.
>>
>> Is it possible to clear the session var when leaving the page?
>>
>> Marco
>
> I've had to do this before and simply added a body onunload event
> listener to the page:
>
> <body onunload="clearSession()">
>
> Calls a javascript function:
>
> function clearSession(){
> // here make a request to a PHP script called clear_session.php
> }
>
> Finally the clear_session.php script:
>
> <?php
> unset($_SESSION['myVar'];
> ?>
>
> Martin.
>

Which is not at all reliable. Better to code the server side properly.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: how to unset session variable when leaving page [message #174741 is a reply to message #174737] Fri, 01 July 2011 11:50 Go to previous messageGo to next message
dougatmilmacdotcom is currently offline  dougatmilmacdotcom
Messages: 24
Registered: May 2011
Karma: 0
Junior Member
In article <57e91bee-b606-473f-8ba8-020927f01a53(at)d1g2000yqm(dot)googlegroups(dot)com>, Martin <warwound(at)gmail(dot)com> wrote:
> On Jun 27, 7:48=A0pm, Co <vonclausow...@gmail.com> wrote:
[...]
>> Is it possible to clear the session var when leaving the page?
>>
> I've had to do this before and simply added a body onunload event
> listener to the page:
>
> <body onunload="clearSession()">
>
> Calls a javascript function:

Which obviously doesn't work _at all_ if the visitor's browser has javascript
disabled.
Re: how to unset session variable when leaving page [message #174742 is a reply to message #174739] Fri, 01 July 2011 12:00 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Fri, 01 Jul 2011 05:36:55 -0400, Jerry Stuckle wrote:

> On 6/30/2011 11:41 PM, Martin wrote:
>> On Jun 27, 7:48 pm, Co<vonclausow...@gmail.com> wrote:

>>> [how do I clear a session when leaving a page]

>> [onunload javascript event calling a server side script]

> Which is not at all reliable. Better to code the server side properly.

Yes it's unreliable, and in this case there is a server side solution
(after all, the server knows which page is being requested, and the
referring page, and the contents of any post or get data, so in this case
there is a server side solution).

However, concerns about relying on javascript aside, using an unload
javascript event to call back to the server when the page is unloaded is
as far as I know the only active method that can be used to signal that
the user has browsed away from a web page, as opposed to passive methods
based on session timeouts. Yes, I agree that it is not perfect, and it
needs to be backed up with a session timeout anyway, but it's the only
mechanism that provides for even a chance of the visitor's browser
actively telling the server "byebye".

Unless, of course, you have a better method to suggest to us.

There is I believe another flaw in the logic that underlies the OP's
code, and it's a flaw that caught me out when I started using sessions.
He needs to consider the possibility that his visitor will have multiple
pages from his website open at once in different tabs, and these will all
be part of the same session.

Thus it is possible for:

request page 1 from google search [lastPage = null, ref = google]
request page 2 from page 1 [lastPage = 1, ref = 1]
request page 3 from page 2 [lastPage = 2, ref = 2]
request page 4 from page 1 [lastPage = 3, ref = 1]
request page 5 from page 2 [lastPage = 4, ref = 2]

If he is tracking the current page in his session data contents, then at
the point page 5 is requested, he may be assuming the visitor is coming
from page 4 unless he also tracks the referrer at each request. I didn't
see any indication in his code that he does this.

Of course, it may not matter, but then if it doesn't matter, I wonder why
he's worrying about the issue of unsetting session data when the users
browses to another page anyway.

Rgds

Denis McMahon
Re: how to unset session variable when leaving page [message #174743 is a reply to message #174742] Fri, 01 July 2011 16:35 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 7/1/2011 8:00 AM, Denis McMahon wrote:
> On Fri, 01 Jul 2011 05:36:55 -0400, Jerry Stuckle wrote:
>
>> On 6/30/2011 11:41 PM, Martin wrote:
>>> On Jun 27, 7:48 pm, Co<vonclausow...@gmail.com> wrote:
>
>>>> [how do I clear a session when leaving a page]
>
>>> [onunload javascript event calling a server side script]
>
>> Which is not at all reliable. Better to code the server side properly.
>
> Yes it's unreliable, and in this case there is a server side solution
> (after all, the server knows which page is being requested, and the
> referring page, and the contents of any post or get data, so in this case
> there is a server side solution).
>
> However, concerns about relying on javascript aside, using an unload
> javascript event to call back to the server when the page is unloaded is
> as far as I know the only active method that can be used to signal that
> the user has browsed away from a web page, as opposed to passive methods
> based on session timeouts. Yes, I agree that it is not perfect, and it
> needs to be backed up with a session timeout anyway, but it's the only
> mechanism that provides for even a chance of the visitor's browser
> actively telling the server "byebye".
>
> Unless, of course, you have a better method to suggest to us.
>

You code your scripts so they don't depend on the session being cleared
when the user leaves the page. Rather, I look at the incoming data to
see how the page needing the session data was requested, and start from
scratch if it's a new request, i.e. to a search page. Another way when
you have multiple pages in a sequence is to place a token in a hidden
field on each page, identifying where the user is in the series. Not
secure, but it doesn't look like the user is looking for security here,
anyway.

I've never had to worry about clearing the session data when the user
leaves the page.

> There is I believe another flaw in the logic that underlies the OP's
> code, and it's a flaw that caught me out when I started using sessions.
> He needs to consider the possibility that his visitor will have multiple
> pages from his website open at once in different tabs, and these will all
> be part of the same session.
>
> Thus it is possible for:
>
> request page 1 from google search [lastPage = null, ref = google]
> request page 2 from page 1 [lastPage = 1, ref = 1]
> request page 3 from page 2 [lastPage = 2, ref = 2]
> request page 4 from page 1 [lastPage = 3, ref = 1]
> request page 5 from page 2 [lastPage = 4, ref = 2]
>
> If he is tracking the current page in his session data contents, then at
> the point page 5 is requested, he may be assuming the visitor is coming
> from page 4 unless he also tracks the referrer at each request. I didn't
> see any indication in his code that he does this.
>
> Of course, it may not matter, but then if it doesn't matter, I wonder why
> he's worrying about the issue of unsetting session data when the users
> browses to another page anyway.
>
> Rgds
>
> Denis McMahon

Other than the fact the HTTP_REFER is not reliable either, it's ok.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: how to unset session variable when leaving page [message #174745 is a reply to message #174741] Fri, 01 July 2011 21:58 Go to previous message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
On Fri, 01 Jul 2011 11:50:04 GMT, Doug Miller wrote:

> In article
> <57e91bee-b606-473f-8ba8-020927f01a53(at)d1g2000yqm(dot)googlegroups(dot)com>,
> Martin <warwound(at)gmail(dot)com> wrote:
>
>> On Jun 27, 7:48=A0pm, Co <vonclausow...@gmail.com> wrote:
>
> [...]
>
>>> Is it possible to clear the session var when leaving the page?
>>
>> I've had to do this before and simply added a body onunload event
>> listener to the page:
>>
>> <body onunload="clearSession()">
>>
>> Calls a javascript function:
>
> Which obviously doesn't work _at all_ if the visitor's browser has
> javascript disabled.

Or crashes.

--
86. I will make sure that my doomsday device is up to code and properly
grounded.
--Peter Anspach's list of things to do as an Evil Overlord
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: debugging with NetBeans
Next Topic: barcode reader integration in web application
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 10:59:32 GMT 2024

Total time taken to generate the page: 0.02440 seconds