doubledee Posted July 25, 2011 Share Posted July 25, 2011 I have a log-in script that starts off... <?php // Initialize a session. session_start(); And many, many lines below this starting code is... if ($_SERVER['REQUEST_METHOD']=='POST'){ // HANDLE FORM. * // Check for Member Record. if (mysqli_stmt_num_rows($stmt)==1){ // Member found. other code here... $_SESSION['loggedIn'] = TRUE; $_SESSION['memberEmail'] = $memberEmail; $_SESSION['memberName'] = $memberName; So how is it that when I step through my code, the $_SESSION variables are getting populated the minute my code hits session_start(); ?? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/242696-_session-initializing-with-values-before-assignment/ Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 If they exist prior to that, then they have already been created. Close your browser and start again. If that doesn't work, delete your browser cache. (If the second option works, look at your php.ini for session.cache_expire and see what it says there. http://www.php.net/manual/en/session.configuration.php#ini.session.cache-expire Quote Link to comment https://forums.phpfreaks.com/topic/242696-_session-initializing-with-values-before-assignment/#findComment-1246512 Share on other sites More sharing options...
btherl Posted July 25, 2011 Share Posted July 25, 2011 The whole purpose of $_SESSION is that the values are populated when you call session_start(). $_SESSION is for you to store data that will be available every time the script runs. If you want to clear that data, you can use the sample code here: http://php.net/manual/en/function.session-destroy.php Usually you would run code like that when someone logs out. Quote Link to comment https://forums.phpfreaks.com/topic/242696-_session-initializing-with-values-before-assignment/#findComment-1246549 Share on other sites More sharing options...
doubledee Posted July 25, 2011 Author Share Posted July 25, 2011 The whole purpose of $_SESSION is that the values are populated when you call session_start(). $_SESSION is for you to store data that will be available every time the script runs. If you want to clear that data, you can use the sample code here: http://php.net/manual/en/function.session-destroy.php Usually you would run code like that when someone logs out. I made this thread because my "Members-Only" area wasn't working and I thought it might have something to do with the $_SESSION variables being populated before they were assigned values from my form. Turns out the issue was that I wasn't starting the $_SESSION on the "Members_Only" page. OOPS! As far as your second comment, so you are saying that I'd likely only need to destroy the session variables when a person logs out? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/242696-_session-initializing-with-values-before-assignment/#findComment-1246569 Share on other sites More sharing options...
btherl Posted July 25, 2011 Share Posted July 25, 2011 Yes, destroying the session is a good idea when someone logs out. And probably a bad idea in any other situation. The reason is it's difficult to keep track of all the data you might have put in $_SESSION while someone was logged on. And you want to make sure no-one else who logs in later can access that same data. So it's safest just to get rid of it all. Was the problem that you hadn't called session_start() on the members only page, and then nothing set on that page was accessible from other pages? Quote Link to comment https://forums.phpfreaks.com/topic/242696-_session-initializing-with-values-before-assignment/#findComment-1246615 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.