Page 1 of 1
Php headers already sent problem
Posted: Fri Sep 14, 2007 9:42 am
by tc
Hi all,
Can someone please help with this headers already sent problem. I have created a few pages that require users to log in, in order to access the content, I have echoed the page that users should be directed to upon login using
Code: Select all
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success_login.php">';
The success_login page is appearing but the problem is that the session code does not seem to be working;
Warning: Cannot send session cache limiter - headers already sent (output started at c:\ibserver\www\site\success_login.php:2)
I have the code
at the top of the page and also
Code: Select all
Welcome back <?echo $_SESSION[‘Username’]; ?>
but its just not working...im quite new to php so i dont know how to go about troubleshooting this problem. Any suggestions will be welcome. Thanks.
Re: Php headers already sent problem
Posted: Fri Sep 14, 2007 1:16 pm
by aliasxneo
tc wrote:Hi all,
Can someone please help with this headers already sent problem. I have created a few pages that require users to log in, in order to access the content, I have echoed the page that users should be directed to upon login using
Code: Select all
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success_login.php">';
The success_login page is appearing but the problem is that the session code does not seem to be working;
Warning: Cannot send session cache limiter - headers already sent (output started at c:\ibserver\www\site\success_login.php:2)
I have the code
at the top of the page and also
Code: Select all
Welcome back <?echo $_SESSION[‘Username’]; ?>
but its just not working...im quite new to php so i dont know how to go about troubleshooting this problem. Any suggestions will be welcome. Thanks.
When you use the echo command data is sent out from the server to the browser, and before this data comes the headers, and therefor the headers are also being sent out at the same time. Sessions in PHP use the "Cookie" field in the HTTP Header and therefor require that the header not be sent out when being initiated.
To solve your problem you need to have session_start() as the very first line in your code. You cannot call commands which send output (like echo) before starting session if you intend on using a session.
I think the only alternative is
ob_start() which creates an output buffer and doesn't send any data until specified or the script ends. I could be wrong on this though.
Posted: Fri Sep 14, 2007 1:52 pm
by Zoxive
Why use Meta Refresh when you can, redirect before anything is sent to the browser with
Header() ?
Code: Select all
header('Location: http://www.example.com/');
Then you page wont even get to session_start because they will get redirected to the correct page.
Posted: Fri Sep 14, 2007 10:44 pm
by feyd
Output buffering is not a solution to the "headers already sent" problem. It merely, and poorly, sidesteps the real issue: poor script layout.
Use header("Location://path")....
Posted: Sat Sep 15, 2007 12:05 am
by navindhar
I have already encountered this kind of problem. Try to find out any "echo" has been given
by you in your code. If so, try to remove that one because you are redirecting the page,
same time you are echoing something in the current page. It will generate the error. Try
to use - header("Location://path");