redirect using header()
Moderator: General Moderators
redirect using header()
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!
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!
- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
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.
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.
Hope that helps.
~Scott
Code: Select all
<?
if (some_condition) {
Header("Location: some/other/location.whatever");
die();
} else {
blah...
}
?>~Scott
another way to redirect?
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
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
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. 