ending a session

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

ending a session

Post 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
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post 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.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

is this a common method? what do other people do to unset a cookie when someone leaves or closes the webpage?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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.
Post Reply