Page 1 of 1

session_start() and sending headers.

Posted: Wed Feb 07, 2007 4:10 pm
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]

Posted: Wed Feb 07, 2007 4:15 pm
by tecktalkcm0391
try this:

Code: Select all

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

// your codes 

?>

Posted: Wed Feb 07, 2007 4:19 pm
by Jorge
And it worked. As simple as that hu? Thanks a lot. Care to explain what is happening when I do that?

Thanks.

Posted: Wed Feb 07, 2007 4:37 pm
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.

Posted: Wed Feb 07, 2007 4:43 pm
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.

Posted: Wed Feb 07, 2007 5:14 pm
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.

Posted: Wed Feb 07, 2007 5:57 pm
by RobertGonzalez
ob_flush(). But seriously, why can't you handle code first, then output last?

Posted: Wed Feb 07, 2007 7:21 pm
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 :)

Posted: Wed Feb 07, 2007 10:36 pm
by RobertGonzalez
Good job. Glad you got it.