Page 1 of 1

headers already sent

Posted: Fri Jun 29, 2007 7:51 am
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

Posted: Fri Jun 29, 2007 7:58 am
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

Posted: Fri Jun 29, 2007 7:58 am
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!

Posted: Fri Jun 29, 2007 8:06 am
by mentor
What do you have on line 17 in message.php?

Posted: Sat Jun 30, 2007 11:56 am
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:

Posted: Sat Jun 30, 2007 12:03 pm
by feyd
clean.php doesn't appear to be buffering anything if ob_end_flush() is firing that notice.

Posted: Sat Jun 30, 2007 12:18 pm
by m2babaey
so what's the solution?
this is the exact clean.php:

Code: Select all

<?php
ob_end_flush(); 
?>

Posted: Sat Jun 30, 2007 12:27 pm
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.

Posted: Sat Jun 30, 2007 4:15 pm
by superdezign

Posted: Mon Jul 02, 2007 11:33 am
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;
?>

Posted: Mon Jul 02, 2007 12:53 pm
by m2babaey
why they are not working?
even ob_end_clean() didn't work

Posted: Mon Jul 02, 2007 2:25 pm
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.

Posted: Mon Jul 02, 2007 3:53 pm
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.

Posted: Tue Jul 03, 2007 4:28 am
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.

Posted: Tue Jul 03, 2007 6:35 am
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.