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!
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?
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?
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.
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?