PHP Session question...

PHP Session question...

am 12.04.2008 01:14:37 von dysfunct

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:
session_start();

?>
....
Hello, 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...

am 12.04.2008 03:48:21 von Jerry Stuckle

dysfunct wrote:
> 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:
> > session_start();
>
> ?>
> ...
> Hello, > 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?
>

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.
jstucklex@attglobal.net
==================

Re: PHP Session question...

am 12.04.2008 04:00:41 von luiheidsgoeroe

On Sat, 12 Apr 2008 03:48:21 +0200, Jerry Stuckle
wrote:
>> I can't use session_start() in login.php; it gives me an error saying
>> http headers have already been sent.
>> Any suggestions?
> You need session_start() at the beginning of EVERY page which uses
> sessions.

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