session_start() and sending headers.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jorge
Forum Newbie
Posts: 14
Joined: Tue Mar 21, 2006 8:36 pm

session_start() and sending headers.

Post by Jorge »

Hello all,

I have implemented a login in php5 but get the following error when I call session_start() once the user has been authenticated:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/jorge/development/ITERation/webapp/layout/layout_top.php:22) in /home/jorge/development/ITERation/webapp/pages/login.php on line 51
Now, what I understand so far from the research I've done is that previous loading of html and javascript logic have caused a header to already have been sent, thus I cannot send another one when executing session_start(). Is there a way around this? Should I be creating my own header before displaying anything and if so, how? If this is not the answer, then how can I work around this without getting rid of my previous displays?

Thanks for your time,

- Jorge[/quote]
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

try this:

Code: Select all

<?php 
ob_start(); //very top line  [starts output buffer]

// your codes 

?>
Jorge
Forum Newbie
Posts: 14
Joined: Tue Mar 21, 2006 8:36 pm

Post by Jorge »

And it worked. As simple as that hu? Thanks a lot. Care to explain what is happening when I do that?

Thanks.
Jorge
Forum Newbie
Posts: 14
Joined: Tue Mar 21, 2006 8:36 pm

Post by Jorge »

Ok, there's some drawback to this work around. Once the buffer is full login will no longer work and spew the following error:
Allowed memory size of 8388608 bytes exhausted (tried to allocate 14592 bytes) in /home/jorge/development/ITERation/webapp/pages/login.php on line 49
I tried using ob_clean() to empty the buffer but no dice.
And I'm not sure but I don't thikn the sessions is identified once I refresh.
Anyhow, any work around this method or another solution?

Thanks.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

DO NOT USE OUTPUT BUFFERING AS A BANDAID TO BAD CODE!

Headers already sent means that you output something to the browser before calling session_start(). There are a handful of functions that will give you this error (which has been discussed in great length throughout our forum by the way) including session_start(), header() and setcookie(). Please read the manual on these functions for more information on what headers are and why you cannot output anything to the browser before calling those functions.
Jorge
Forum Newbie
Posts: 14
Joined: Tue Mar 21, 2006 8:36 pm

Post by Jorge »

Yes, I am already aware of why this problem is occuring. Like I said, the searches I've done on this have revealed to me that the previous HTML calls that I do to generate the layout is the reason why a header is already sent in the first place. I post an HTML layout before the login logic is called. So really, my question is how can I suse session_start() when their is HTML logic being read beforehand? What would be the proper way of doing this?

I read somewhere that sending my own header at the very beginning and then changing it is a possible approach but I am unsure how that can be done.

Also, becasue I've used ob_start() I know have a full buffer that I cannot seem to empty, even upon restart of my system. I've tried ob_end_clean() on all open buffers but it doesn't seem to go away. Any hints on that?

Thanks.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

ob_flush(). But seriously, why can't you handle code first, then output last?
Jorge
Forum Newbie
Posts: 14
Joined: Tue Mar 21, 2006 8:36 pm

Post by Jorge »

ok, I took a nap, woke up, skimmed through my code, felt stupid, fixed the problem and now it works.
Sleep deprevation, it's a horrible thing.

And yes, I can and did handle the code first and now it works :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Good job. Glad you got it.
Post Reply