Jump to content

$_SESSION initializing with values before assignment


doubledee

Recommended Posts

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

 

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

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.

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

 

 

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.