Php Session Help Please!!!
- dag10
-
dag10
- Member since: Jan. 15, 2006
- Offline.
-
- Forum Stats
- Member
- Level 05
- Programmer
I am setting up a log-in thing in php for my website.
It logs me in, and sets the session. all good. but when I chick a link to go anywhere else on my site, the session is earased. How?! do you need session_start()???
I tried that, but i get the following errors:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /misc/17/351/635/051/3/user/web/minipenguin.n et/accounts/index.php:1) in /misc/17/351/635/051/3/user/web/minipenguin.n et/standards/side.php on line 6
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /misc/17/351/635/051/3/user/web/minipenguin.n et/accounts/index.php:1) in /misc/17/351/635/051/3/user/web/minipenguin.n et/standards/side.php on line 6
PLZ HELP!!! Thnx in advance.
- Pilot-Doofy
-
Pilot-Doofy
- Member since: Sep. 13, 2003
- Offline.
-
- Send Private Message
- Browse All Posts (12,142)
- Block
-
- Forum Stats
- Member
- Level 37
- Musician
Let me answer your question first of all. Yes, you do need a call to session_start() every time you call a page with a session in it. Further more, sense it is a header modification, headers cannot have been sent to the browser before calling this function.
As you can see, modifying the header (or trying to at least) after they have already been sent to the browser causes an error. Well, there are two ways you can handle that.
One way, and the simplest way is probably to call to session_start() at the very beginning of each page. For example:
<?php
session_start();
?>
<html>....
Like that. If you choose this method, NOTHING can come before the call to session_start(), and I mean NOTHING! (This isn't entirely true, but pretend it is in order to save yourself a lot of trouble).
Second option, which is what I would encourage, is to use an output buffer. When you send output to a browser, you are sending header information along with that output. So, lets say you have this:
<html> blah blah
<?php
session_start();
?>
This raises an error because output has been sent before the call to session_start(). Well, we can avoid that by using an output buffer. What an OB (output buffer) does is sort of "hold" all the information that is to be sent until the page finishes execution. That's what it means, in a very simplistic way to "buffer" something.
You could do this alternatively:
<?php
ob_start();
?>
<html> blah blah
<?php
session_start();
?>
This would be perfectly fine and wouldn't cause an error, because we defined an output buffer before we sent anything to the browser. I don't feel like explaining output buffers anymore, so you can follow up on them here:
http://newgrounds.com/bbs/topic.php?id=539687
So yeah, pip pip cheerio and all that.

