Page 1 of 1

redirect using header()

Posted: Sun Jan 12, 2003 11:59 pm
by Chinh
Hi!

Im new to PHP and im stuck in one problem is that I can't do the REDIRECT command with PHP by using [b]header()[/b]. In my situation, after checking for a valid member in the login page, I redirect the user to another page with [b]header($myurl)[/b] but I verceived the following error:

[b]Warning: Cannot add header information - headers already sent by (output started at D:\software\testphp\redirect.php:13) in D:\software\testphp\redirect.php on line 14
[/b]
and it stop.

Could you please tell me how to avoid this error!

Thank you!

Posted: Mon Jan 13, 2003 1:11 am
by mydimension
header information can only be sent to the browser before you send anything else tou it. PHP by designing automatically finishes sending its headers when you start your normal outout (via print, echo, etc.). to avoid this error the simple way just make sure your calls to header() preceed any output to the browser. that also includes possible blank lines at the beggining of the script or any included script.

Posted: Mon Jan 13, 2003 1:14 am
by skehoe
Be sure that you aren't sending anything to the browser prior to the header() command and also be sure to kill the script immediately after sending the redirect.

Code: Select all

<?

if (some_condition) &#123;
    Header("Location: some/other/location.whatever");
    die();
&#125; else &#123;
    blah...
&#125;
?>
Hope that helps.

~Scott

another way to redirect?

Posted: Mon Jan 13, 2003 1:18 am
by Chinh
Hi!

Thanks for your help!
I also have one question is that is these any other way to redirect the site instead of use header?

Chinh

Posted: Mon Jan 13, 2003 1:24 am
by skehoe
I think there's a way to do it with a meta tag or you could always use javascript, too. The easiest way is to use the location() command though.

~Scott

thanks!

Posted: Mon Jan 13, 2003 1:31 am
by Chinh
Thanks for you help!

Posted: Mon Jan 13, 2003 2:51 am
by Elmseeker
Of course you could use ob_start(); at the top of your script to avoid this problem and use headers no matter what, IF output buffering is turned on in php.ini that is. ;)

Posted: Mon Jan 13, 2003 3:50 pm
by Kriek
I concur. ob_start() and ob_end_flush() have saved my life more times than I can count. :D

Posted: Mon Jan 13, 2003 4:05 pm
by Elmseeker
Although ob_end_flush() isn't needed in MOST cases because it is called automatically at the end of script execution by PHP itself when output buffering is turned on either in the php.ini or by calling ob_start(). You only really ever need to call if you need to flush for some wierd reason before a script is complete. For what he needs to do ob_start() alone is fine and no flush is needed. I use the same technique on my page as a matter of fact. :)