redirect using header()

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
Chinh
Forum Newbie
Posts: 3
Joined: Sun Jan 12, 2003 11:59 pm

redirect using header()

Post 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!
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post 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.
User avatar
skehoe
Forum Commoner
Posts: 59
Joined: Sun Dec 22, 2002 5:57 am
Location: Denver

Post 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
Chinh
Forum Newbie
Posts: 3
Joined: Sun Jan 12, 2003 11:59 pm

another way to redirect?

Post 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
User avatar
skehoe
Forum Commoner
Posts: 59
Joined: Sun Dec 22, 2002 5:57 am
Location: Denver

Post 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
Chinh
Forum Newbie
Posts: 3
Joined: Sun Jan 12, 2003 11:59 pm

thanks!

Post by Chinh »

Thanks for you help!
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post 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. ;)
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

I concur. ob_start() and ob_end_flush() have saved my life more times than I can count. :D
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post 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. :)
Post Reply