headers already sent

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
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

headers already sent

Post by m2babaey »

Hi
We talked in another thread ( redirect instead of die ) I appreciate your answers there but decided to try this way, and opened a new thread because the subject is different and that thread had became too long.
account.php is going to be a membersOnly area.
when visitors that have not logged in, click account.php, I redirct them to a login form on message.php
This is message.php itself ( please have a look. it's safe)
In the centric td ( table data or cell), I display the login form. like this
I know about the "headers already sent" and session_start. so included the session_start on top of message.php. But users are receiving this error upon login:

Warning: Cannot modify header information - headers already sent by (output started at g:\programs(2)\easyphp1-8\www\ha\previous\new folder\another\htdocs\message.php:17) in g:\programs(2)\easyphp1-8\www\ha\previous\new folder\another\htdocs\global.php on line 27

Warning: Cannot modify header information - headers already sent by (output started at g:\programs(2)\easyphp1-8\www\ha\previous\new folder\another\htdocs\message.php:17) in g:\programs(2)\easyphp1-8\www\ha\previous\new folder\another\htdocs\global.php on line 28
this is line 26 to 30 of global.php and I'm not finding a session_start there.

Code: Select all

function redirect($url) {
	header("HTTP/1.0 302 Found");
	header("Location: $url");
	exit();
}
why that happens and how to fix it?
thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Interestingly, we have a tutorial on this very subject which is years old but still rings as true as ever.

Have a read: viewtopic.php?t=1157
User avatar
idevlin
Forum Commoner
Posts: 78
Joined: Tue Jun 26, 2007 1:10 pm
Location: Cambridge, UK

Post by idevlin »

A redirect via PHP needs to be the very first thing you do before anything at all is ouput. This is your problem.

The check for if they are logged in or not (and the potential subsequent redirect) needs to be the very first thing on your page, before you echo <html> or anything like that.

**EDIT**
I suggest following what feyd posted above. He knows better!
mentor
Forum Contributor
Posts: 100
Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan

Post by mentor »

What do you have on line 17 in message.php?
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Post by m2babaey »

Hi
It seems that it's causing another problem. clean.php is bufferring the output. but this error:
Notice: ob_end_flush(): failed to delete and flush buffer. No buffer to delete or flush. in g:\programs(2)\easyphp1-8\www\ha\previous\new folder\another\htdocs\clean.php on line 2

Notice: ob_end_flush(): failed to delete and flush buffer. No buffer to delete or flush. in g:\programs(2)\easyphp1-8\www\ha\previous\new folder\another\htdocs\clean.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at g:\programs(2)\easyphp1-8\www\ha\previous\new folder\another\htdocs\clean.php:2) in g:\programs(2)\easyphp1-8\www\ha\previous\new folder\another\htdocs\global.php on line 27

Warning: Cannot modify header information - headers already sent by (output started at g:\programs(2)\easyphp1-8\www\ha\previous\new folder\another\htdocs\clean.php:2) in g:\programs(2)\easyphp1-8\www\ha\previous\new folder\another\htdocs\global.php on line 28
where i am wrong? :oops: :oops:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

clean.php doesn't appear to be buffering anything if ob_end_flush() is firing that notice.
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Post by m2babaey »

so what's the solution?
this is the exact clean.php:

Code: Select all

<?php
ob_end_flush(); 
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://de2.php.net/function.ob_end_flush wrote:ob_end_flush — Flush (send) the output buffer and turn off output buffering
You cannot send headers after output has been sent to the client.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

This is not the correct way but a work around would be too do this for redirecting:

Code: Select all

<?php
echo "<meta http-equiv='refresh' content='0; url=$url'>";
exit;
?>
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

Post by m2babaey »

why they are not working?
even ob_end_clean() didn't work
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

m2babaey wrote:why they are not working?
even ob_end_clean() didn't work
Then stop outputting data before the redirection and be done with it.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try to understand HTTP a little.

The user (client): "May I have a page?"
The web site (server): "Let me see if I can find it" (/GET REQUEST)
The web site (server): "Ok, I found it. Here it is" (/200 RESPONSE)

Now if you were at a restaurant and you asked for a meal from your server, and your server served it to you, then went back to the kitchen and tried to serve your exact request to another table, that wouldn't work, would it? No, because he already served it to you.

When you push output to the browser you have essentially served the page. The server can no longer send response headers because the response was already sent out after the headers have been sent by the server.

The fix for your problem: Don't send output to the browser before messing with the response headers.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

get rid aof any spaces on the top of the page. This may help.

for example if you have

Code: Select all

<?php //this has one line space
ob_end_flush(); 
?>
put it like this

Code: Select all

<?php ob_end_flush(); ?>
make sure no spaces are at the top of the page. Headers errors do come if you have spaces at the top of your page.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

gurjit wrote:get rid aof any spaces on the top of the page. This may help.

for example if you have

Code: Select all

<?php //this has one line space
ob_end_flush(); 
?>
put it like this

Code: Select all

<?php ob_end_flush(); ?>
make sure no spaces are at the top of the page. Headers errors do come if you have spaces at the top of your page.
Sorry to say, but you're example is incorrect. Nothing inside of PHP tags is counted out output until an output function is called. Your suggestion about the spaces is accurate, but your example is not.
Post Reply