header
 
     
 
pixel
pixel pixel

Redirect a URL using PHP

Redirecting to a new URL in PHP is done with the header() function. The command is used as follows:
<?php
header
("Location: http://www.redirecturl.com/");
?>
Note - you must call this function before you output anything, otherwise the server automatically generates a header for you and you'll receive a "Cannot modify header information - headers already sent" error message. You can't even output a space. So make sure your <?php begins at the very start of the document and that you don't print or echo anything before your redirect.

Unhelpful Helpful Rating 4.1 (score out of 5, no. of ratings: 69)
Comments
Comment by dlk on 2005-02-18
isn't there another possibility? i can't use header (): error: Cannot modify header information - headers already sent by (output started at

Comment by veto on 2005-02-28
You can always use header() ...just don't print anything on the page before the redirect. Why would you want to display anything on the page when it's redirecting?

Comment by scott on 2005-03-05
Yes, something else is needed. I'm detecting browsers in an PHP page, then need to redirect to another page if browser=X ... I get the same message as 'dlk' (i.e. Warning: Cannot modify header information - headers already sent by (output started at..."

Comment by scott on 2005-03-06
Aha ... the only thing needed is more PHP experience! ***IMPORTANT*** Not only must 'you call this function before ANY output', but it MUST go at the very top of the (in my case) HTML file. Before, even, the tag. Do that and it works as advertised.

Comment by scott on 2005-03-06
CORRECTION: Should read "Before, even, the "HTML" tag.

Comment by teddy on 2005-04-01
if i put session_start() on the top of the page then why cant i using header() in the subsequent code, say at the end of the code. HELP ME!!!

Comment by chris on 2005-04-18
Here is what I found that works as a comment on the PHP manual under headers_sent() function: \r\n"; } } ?>

Comment by eswar on 2005-04-20
when i use header() to redirect a url , i am getting an error message. How to solve it. please help me in this situation

Comment by Jonny on 2005-04-30
ive already used a header/redirect script is there another one i could use in the same file?

Comment by Yohan on 2005-07-05
how to specify the target if we're using framed pages? can anyone help?

Comment by ghostwind on 2005-07-31
\"You can always use header() ...just don\'t print anything on the page before the redirect. Why would you want to display anything on the page when it\'s redirecting?\" You need to use output buffering. In effect, what you are doing when you run the redirect through php, you are sending information after the header info has already been sent to the server creating the error message. Regardless of where the script is on the page, you should use \"ob_start()\" at the beggining of redirect, and \"ob_end_flush()\" at the end

Comment by Eugene on 2005-08-02
Why would you want to display anything on the page when it\'s redirecting?\" You wouldnt but what if someone was doing a hit counter. To count how many times that url was being visited?

Comment by Paul on 2005-08-12
THis info is just what i needed

Comment by mendy on 2005-08-24
I'm trying to run this code & the first statement is not working. PLZ check it for me. if(($_POST["use"] == "$user") && ($_POST["passwd"] == "$passwd")) { $url = '/ediover.php'; header("Location:$url"); } It doesn't redirect the page. It gives me thi warning :-Warning: Cannot modify header information - headers already sent by (output started at C:\apache2triad\htdocs\itd\login.php:14) in C:\apache2triad\htdocs\itd\login.php on line 35

Comment by Okuwa Nwanze on 2005-12-02
Warning: Cannot modify header information - headers already sent by (output started at

Comment by xan on 2005-12-09
i want to check the user name and password before i redirect .. what shall i do ...

Comment by Harish on 2007-03-07
you must call this function before you output anything, otherwise the server automatically generates a header for you and you'll receive a "Cannot modify header information - headers already sent" error message. You can't even output a space. THATS TRUE.. EXACTLY ITS ONE FROM THOUSAND..

Comment by wow gold on 2008-06-17
wow gold kaufen Gamesavor hat wow gold und liefert schnell & billig wow gold kauf (WoW Geld).Kaufen wow gold jetzt and erhalten Gold WoW schnell.

Comment by Dilz on 2007-07-21
Wow Super Please Give some more info about php tips www.superfilez.com

Comment by Mohsin Raza on 2007-09-29
Its very impressive way, ive already used a header/redirect script is there another one i could use in the same file?

Post a Comment
Name
Email
(optional)
Comment
RatingUnhelpful Helpful
Security Image* (this is just to prevent spam submissions)
Security Image