Page 1 of 1

ending a session

Posted: Mon Dec 20, 2004 1:46 am
by C_Calav
hi guys,

im setting this session.

Code: Select all

<?php
else 
{ 
setcookie("cartId", session_id(), time() + ((3600 * 24) * 30)); 
return session_id(); 
} 
?>
can anyone tell me if (and how) i can unset this session once the user leaves the site or closes the window or something along those lines?

thanx

Posted: Mon Dec 20, 2004 5:16 am
by gite_ashish
You can use call JavaScript function in which u can unset cookie. You can call this JavaScript function on BODY's onUnload event.

Posted: Tue Dec 21, 2004 1:58 pm
by C_Calav
is this a common method? what do other people do to unset a cookie when someone leaves or closes the webpage?

Posted: Tue Dec 21, 2004 3:11 pm
by rehfeld
well if you want to clear the cookie when they leave or close thier browser, youll need to use javascript. theres not other way

but not everyone has js turned on

if you dont set an expires value(default session behavior) the browser will clear the cookie when they close the browser.

if you want the both the browser to clear the cookie AND timeout the session after a period of inactivity(whichever comes first), you need to do the timeout yourself.
for example, everytime they request a page from your website you update a session or database last activity value.

then, you check that value every page request. if the last time they were active was more than x minutes ago, then you stop accepting the session and start a new one, maybe making them log in again or whatever.