Session Destroy

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
Ross2376
Forum Newbie
Posts: 19
Joined: Mon May 30, 2005 12:03 am

Session Destroy

Post by Ross2376 »

How can I make my page destroy a session when a user navigates away from that certain page?


Thanks,

Ross
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

Code: Select all

if (!eregi("yourfile.php", $_SERVER['SCRIPT_NAME'])) { 
   session_destroy();
}
this supposes that you move to another page with different file.
but you can modify to meet your need.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Its better to clear the entire thing:

Code: Select all

session_unset();
$_SESSION = array();
session_destroy();
setcookie(session_name(),"",0,"/");
The last line destroys the session from the cookie if present.
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

thanks! I should do this :)
Ross2376
Forum Newbie
Posts: 19
Joined: Mon May 30, 2005 12:03 am

Post by Ross2376 »

Wouldn't I have to apply that code to every page I don't want in the session?

If so, is there a way where I can have my script recognize when someone is coming from a "non-sessioned" page and destroy the open session there?
Ross2376
Forum Newbie
Posts: 19
Joined: Mon May 30, 2005 12:03 am

Post by Ross2376 »

Um hello?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Ross2376 wrote:Wouldn't I have to apply that code to every page I don't want in the session?

If so, is there a way where I can have my script recognize when someone is coming from a "non-sessioned" page and destroy the open session there?
supposing you would: just put that in a new .php file and include() or require() it on the pages you want to destroy the session.

if you only have a few session vars set that you want to kill, you could always just unset() them.
Ross2376
Forum Newbie
Posts: 19
Joined: Mon May 30, 2005 12:03 am

Post by Ross2376 »

I'm wanting to destroy a session (so it will reset itself) when a user navigates away from a single page. When they go back to the sessioned page, it will be reset. It's a thumb gallery. You can see it at AoD Media Page.

There is a counter that monitors what pics have already been displayed. It stores that in the session and then when the user clicks to next page, it just reloads same page only with updated counter (due to session). But when the user moves away from the page, I want the session to be destroyed so if they come back to the gallery, they wont be started in the middle of the gallery. Currently, the only session destroy happens at the end of the gallery.

By the way, those current thumbs are only test images. We will be using screenshots. Please advise on how to destroy the session when the user moves from the gallery page. I would prefer not to add session_destroy() to every page if I can just use something on the one gallery page.

Thanks
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

unset a session var you could. Put the visited pictures in an array an serialize it I would. Save the serialized array in a session and check against it on pages with more pictures you should.

the session var on pages with no pictures you should unset.
Ross2376
Forum Newbie
Posts: 19
Joined: Mon May 30, 2005 12:03 am

Post by Ross2376 »

No offense, but you sound like yoda. I have no clue what you are talking about. Can you give me an example?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

it was yoda speak night of course!

look up serialize and unset on php.net
Post Reply