Php headers already sent problem

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
tc
Forum Newbie
Posts: 3
Joined: Wed Aug 29, 2007 9:46 am

Php headers already sent problem

Post 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

Code: Select all

session_start()
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.
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Re: Php headers already sent problem

Post 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

Code: Select all

session_start()
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.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
navindhar
Forum Newbie
Posts: 6
Joined: Fri Sep 14, 2007 10:59 am

Use header("Location://path")....

Post 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");
Post Reply