View Full Version : SESSION not working =(

05-12-2007, 07:03 PM
Okay, as the topic says, I am having a problem with the SESSION thing-a-ma-jig. . .

I can get the session to set with no error messages, the cookie on the comp points to the correct session (of course) , and when I "print $_SESSION['username'];" it works fine. However, when I go to another page and try to bring up the info. . . . NOTHING.

Here is the part of the code in question:

#--------------page one-------------------
# . . . . . . MySQL_query. . . .

$S1 = $row['username'];
$S2 = $row['password'];
$S3 = $row['accesslevel'];
$S4 = $row['webstatus'];
$_SESSION['username'] = $S1;
$_SESSION['password'] = $S2;
$_SESSION['accesslevel'] = $S3;
$_SESSION['webstatus'] = $S4;

print '<a href="./settings/test.php">CLICK ME</a>';

#----------------Page two---------------

if ($_SESSION['username'] == 'Jas'){
print 'hi';
print 'bye';
print $_SESSION['username'];
print $_SESSION['password'];
print $_SESSION['accesslevel'];
print $_SESSION['webstatus'];


Page two is just a test, but you get the picture. The second page does not pull up the info. Anyone know why?

05-12-2007, 07:20 PM
On page 2, place session_start() at the top (just underneath the opening PHP tag).

Hope this helps.

05-12-2007, 07:26 PM
thanks. I'll try that!

05-12-2007, 07:34 PM
Yeah, every page containing session variables must contain the session_start().

05-13-2007, 12:44 AM
You may have run into this problem in that not having that command at the start will actually work in some cases, but not others. Sometimes, the data is still sent, but most times is not, so you'd have inconsistant results with that.
So, yes, just add that at the very start of each page, and that's it.

05-13-2007, 01:36 AM
Or, you can configure your server to start the session automatically.

05-13-2007, 01:42 AM
Or, you can configure your server to start the session automatically.

You can, but not many people have access to the server configuration, or do not have the "know-how" to do so. Perhaps the most effective way to do this would be to manually start the session each time (on each page using session_start() ).

05-13-2007, 05:43 AM
also, the session declaration must be at the very top of the page, so that it will place the appropriate headers into the file



05-13-2007, 11:28 PM
Thanks guys! it works great.