PHP Session question...

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hey all.
Trying to get sessions to work on my app. The issue is this. When the
main page loads I check for a session variable uname to see if the
user is logged in and display a "hello, username" message, or
otherwise use "guest." Pretty straightforward. Then I have a login
form that uses AJAX calls to log the user in. If the login info is
correct, I set  $_SESSION['uname'] to the username. However, if I then
refresh the main page, the session info is gone. Here's a glimpse at
what's going down....

From index.php:

<span>Hello, <?php if(isset($_SESSION['uname'])){
                                   echo $_SESSION['uname'];
                               else {
                                   echo 'Guest';

From login.php:

    $dbh=mysql_connect ...


                //do this on login SUCCESS:
                echo 'Login Successful';
                $_SESSION['uname'] = $_POST['un'];


I can't use session_start() in login.php; it gives me an error saying
http headers have already been sent.

Any suggestions?

Re: PHP Session question...

dysfunct wrote:
Quoted text here. Click to load it

You need session_start() at the beginning of EVERY page which uses sessions.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: PHP Session question...

On Sat, 12 Apr 2008 03:48:21 +0200, Jerry Stuckle  
Quoted text here. Click to load it

Indeed, and you should be able to start a session on every request where  
it's needed BEFORE generating any output. The error message will tell you  
where the output started, get your session_start() before that.
Rik Wasmus

Site Timeline