PDA

View Full Version : SESSION not working =(



Jas
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-------------------
<?php
# . . . . . . MySQL_query. . . .

SESSION_START();
$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---------------
<?php

if ($_SESSION['username'] == 'Jas'){
print 'hi';
}else{
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?

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

Hope this helps.

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

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

djr33
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.

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

thetestingsite
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() ).

boogyman
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



<?php
session_start();
?>

__DOCTYPE_DECLARATION___
<html>
...
</html>

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