A JM Posted October 1, 2009 Share Posted October 1, 2009 When I leave the page I set my $_SESSION variable on does it survive to the next page? If so, how do I clear the $_SESSION variable when I leave the page so that its only available when I'm on a specific page. Thanks. A JM, Link to comment https://forums.phpfreaks.com/topic/176227-question-about-_session-variable/ Share on other sites More sharing options...
Alex Posted October 1, 2009 Share Posted October 1, 2009 $_SESSION is a super-global, it'll only be accessible on pages that have session_start() on them. Note: make sure you call session_start() before any output or you'll get a headers already sent error. Link to comment https://forums.phpfreaks.com/topic/176227-question-about-_session-variable/#findComment-928735 Share on other sites More sharing options...
A JM Posted October 2, 2009 Author Share Posted October 2, 2009 Since the variable would be "super-global" its perfect for keeping my variable but when I leave the page I want it to be deleted. Is there like some type of exit function() to erase the variable? The page that I would be changing to would also have a session_start() on it. Thanks, Link to comment https://forums.phpfreaks.com/topic/176227-question-about-_session-variable/#findComment-928816 Share on other sites More sharing options...
Alex Posted October 2, 2009 Share Posted October 2, 2009 session_destroy()? Or do you mean like: unset($_SESSION['var']) ? Link to comment https://forums.phpfreaks.com/topic/176227-question-about-_session-variable/#findComment-928818 Share on other sites More sharing options...
itaym02 Posted October 2, 2009 Share Posted October 2, 2009 ...when I leave the page I want it to be deleted.... In that case SESSION is the wrong choice! If you really think you need a superglobal variable (which, in most cases is a bad idea). Simply define it in the global scope (outside all function and classes) and then call it anywhere in the script like that: $GLOBAL['var_name'] Link to comment https://forums.phpfreaks.com/topic/176227-question-about-_session-variable/#findComment-928837 Share on other sites More sharing options...
A JM Posted October 2, 2009 Author Share Posted October 2, 2009 I must be confused then on how a $_SESSION variable works. When is a $_SESSION variable cleared - when the user exits the site and closes the browser? A JM, Link to comment https://forums.phpfreaks.com/topic/176227-question-about-_session-variable/#findComment-929028 Share on other sites More sharing options...
cags Posted October 2, 2009 Share Posted October 2, 2009 An individual variable in the $_SESSION array will be cleared when you either unset it, set it equal to NULL or when the Session ends. A Session will end when you either call session_destroy(), when the user closes the browser, or when the Session times out. Link to comment https://forums.phpfreaks.com/topic/176227-question-about-_session-variable/#findComment-929036 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.