Skip to content

PHP redirect header location

Ok finally i managed to write a new article after about a month ( actually i do not even want to think when i wrote last good article for you). Anyway today short article on using headers to redirect vistors to other pages on your site or external site.

To do this you need only one function
header($header, $replace, $http_response_code);

First argument $header is a header itself. We want to use header to redirect visitors so in our case we will use something like “Location: URL” where URL is obviously an url of a page to which you want to redirect visitor. It can be full url (http://some-random-url.com/pages/page.html) or relative URL (pages/page.html)

Boolean argument $replace indicates whatever previous similar headers should be replace or not. By default this value is set to true, so all similar headers are replaced.

The last argument is HTTP response code. While $replace argument maybe not so useful, $http_response_code might come handy, because it allows to select method of redirection. Full list of HTTP response codes can be found at Wikipedia: List of HTTP status codes. We will be interested in 3xx response codes as they are responsible for redirections.

  • 301 – Moved Permanently. This and all future requests should be directed to the given URI.
  • 302 – Moved Temporarily. Usually browsers use it as 303 redirection
  • 303 – See Other. Resource you are looking for can be found under different URL
  • 307 – Temporary Redirect

It maybe a little bit confusing if you are new to this, so remember that only first argument in header function is required, others are optional.

Examples

Here are some valid PHP redirections:

header("Location: http://google.com");
header("Location: pages/page.php");
header("Location: http://google.com", true, 303);
header("Location: http://google.com", true);

Remember also that headers MUST be sent before any output is started. There are no exceptions from this rule, in fact you cannot even sent a blank line to the output, executing code like this:

text
<?php header("Location: ditio.net"); ?>

will throw warning “Cannot add/modify header information – headers already sent by…“. To avoid such situation you can use output buffering or just make sure that nothing was sent to the client.

Published inGeneral

48 Comments

  1. Hey,

    Really it is quite interesting, the above posted information providing a valuable and useful information on PHP redirect header location.

  2. I just wonder what is the different between a php redirect and meta redirect? Maybe they just do the same stuff? Can’t you make some kind of javaredirect instead?

    BR,
    Joan

  3. Hi Joan, that’s a good question, for me technically this are the same kinds of redirection, differences are in the small details.

    Redirection with PHP is done without sending any html to the client, so it is faster solution, but actually what kind of redirect you need in any given situation.

    As for your third question, redirection can be made with javascript, but this one is tricky because still small percentage of users do not use javascript, more over web browsers differently interpret some parts of the JS.

  4. Girish kumar Girish kumar

    hi i am trying to design download page
    after download starts i need to redirect to another
    page
    can you suggest me how it can be done

    thank you

  5. Tough question. To be honest i have no idea how to do it, maybe someone of my readers can tell?

    Unfortunately i can give you only some pointers.
    – Probably you cannot do it using only PHP, because once you send file, you cannot send any headers
    – You can open download page in new window, then in parent window you will be able to redirect user – using JS.

  6. Interesting article …

    I have some questions for you:
    1- Which one is more search engine friendly? (if any)
    2- what is the practical difference between using codes 302, 303 and 307?

    Thanks a lot!

  7. To be honest i have no idea what’s the difference, you are just sending a response code. In the end it’s up to browser/bot to decide how to handle that response.

  8. Yeah that is true!

    So I guess my question was that “if you know how google crawler behaves when it sees this different responses?”

    Thanks anyways! :-)

  9. Well, that’s a question i do not have an answer for. :)

  10. .htaccess file best solution to redirect in this situation.

  11. Ray Dona Ray Dona

    Hi Greg,
    When you use header to redirect to a file, the path to the file appears as the title of the web page. Is there a way of suppressing this, i.e. leave the title blank or have a title of your own.
    Ray

  12. Sometimes if you have a problem with redirecting not working in PHP you can use the exit; statement EG:

    header(“Location: sdfasfa.php”);
    exit;

    If you have some troubles with that.

  13. Very informative article, thank you.

    Are you able to advise how I can use the following code to open in a new window ?

    Many thanks in advance

  14. That’s impossible, as it works on server side.

  15. ramesh ramesh

    Warning: Cannot modify header information – headers already sent by (output started at /xxxxx/sxxxxx/xxxxx/xxxxx/xxxxx.php:7) in /home2/xxxxx/xxxxxx/xxxxxxxxxx/xxxxxxxxxxxx.php on line 1

    i got this error….

    can u explain me..
    and how can i fixed this error
    thank you

  16. Thank you very muCh admin

  17. Hi,
    Ramesh, you can solve this error by moving your header code above, i.e to the beginning of the code. I got error php can’t redirect. do you have any idea to fix it.

  18. Srividya Jyothirmai Srividya Jyothirmai

    I have one doubt sir.

    Can we pass more than one parameter to header. Thn Hw can we reach it? Let me know..

  19. Hi everyone.

    I hate to use the header and I think it is one of the drawbacks for php. Why cannot it be simpler to send a basic redirect to a page. My question is Why complicating the redirection. My advice is to plan your application carefully especially if you are going to use a lot of includes. For example if you do a template of header and footer then the start of the output is your header file. All codes that need to deal with the header will have to be in the header file above any outputs. One of the areas you might need to use the header is using the login form that you wish to place in the body of the html(ops you cannot use it without getting in trouble.). So lots of work around will have to be put in place. One solution is to provide a link for the login page, and then the login page should be simple without any header include (stand a lone application). But this is unpractical for example you want to dynamically provide the login from inside the template if the session is not registered, a login form appears . if session is registered a log out button or link is provided.

    The rules are:
    1) avoid using header and includes unless the header is above the included file
    2)use absolute positioning in css so that when you place output of includes it can be called from any part of the html / php files, especially header and footer.

    for example

    or

    or

    it won’t work if you

    I have to say, I always have problems with the header and sometimes it is just hard work to do basic redirection.

  20. Hi everyone.

    I hate to use the header and I think it is one of the drawbacks for php. Why cannot it be simpler to send a basic redirect to a page. My question is Why complicating the redirection. My advice is to plan your application carefully especially if you are going to use a lot of includes. For example if you do a template of header and footer then the start of the output is your header file. All codes that need to deal with the header will have to be in the header file above any outputs. One of the areas you might need to use the header is using the login form that you wish to place in the body of the html(ops you cannot use it without getting in trouble.). So lots of work around will have to be put in place. One solution is to provide a link for the login page, and then the login page should be simple without any header include (stand a lone application). But this is unpractical for example you want to dynamically provide the login from inside the template if the session is not registered, a login form appears . if session is registered a log out button or link is provided.

    The rules are:
    1) avoid using header and includes unless the header is above the included file
    2)use absolute positioning in css so that when you place output of includes it can be called from any part of the html / php files, especially header and footer.

    for example
    <?php code?>
    <html>
    </html>

    or

    <html>
    <?php code?>
    <header>
    </header>
    </html>

    or
    <html>
    <header>
    <?php code?>
    </header>
    </html>

    it won’t work if you

    <html>
    <header>
    </header>
    <body>
    <?php code for header ?>
    </body>
    < /html >

    I have to say, I always have problems with the header and sometimes it is just hard work to do basic redirection.

  21. A Cheng A Cheng

    Hi everyone now I’ve got this problem as ramesh as well…
    Please help me to solve this problem..
    Warning: Cannot modify header information – headers already sent by (output started at /xxxxx/sxxxxx/xxxxx/xxxxx/xxxxx.php:7) in /home2/xxxxx/xxxxxx/xxxxxxxxxx/xxxxxxxxxxxx.php on line 1

    Thanks

  22. sonal sonal

    i have make project php but when user login the page not go proper output

  23. How cane redirect the page in live program

  24. bozden bozden

    For those non-english web site designers (like myself) “Cannot modify header information – headers already sent” error is common.

    Those programmers must encode the PHP files in UTF-8. But there are two kinds of them. With BOM and without BOM. In with BOM case, the file will have two extra bytes in front of the file, which is not visable in editors. But PHP sends them to the browser. So, although you have the header() function at the very beginning, you get this error.

    I had lots of headackes because of this. For example, the same problem arises in session handling. Session id’s keep changing if you have with BOM.

    So: NEVER use UTF-8 WITH BOM encoding. It is useless for PHP files.

    For editing, DO NOT USE NOTEPAD in windows, because it puts BOM without asking. A very good free editor is Notepad++.

    Hope this helpes.

  25. Usually “Cannot modify header information – headers already sent” is displayed when information/code is sent out before reaching the PHP Header/Location redirect. Even an errant space outputted before the redirect can cause this.

    To keep things simple, try to have a redirect checker first thing in source before any code is outputted (especially before any line breaks and spaces start to appear).

  26. Thanks for info, really helpfull ..

  27. Thanks for the info and its really good to read out and I hope you will keep up the good work in future as well.

  28. No No

    Thanks .. ^_^

  29. vinod vinod

    Thanks a lot….

  30. Kusa Kuti Kusa Kuti

    good one..

  31. This is actually on the list of greater articles of the ones that We’ve stay with me this kind of matter lately. Wonderful deliver the results.

  32. vinitha vinitha

    hi

    I am a beginner in php and i just created a login /registration page.
    In the login page after checking whether the user is valid, i would like to redirect it to the next page.
    i used
    session_start();
    $_SESSION[‘login’]=$uname;
    echo “session=”. $_SESSION[‘login’];
    if (!(isset($_SESSION[‘login’]) && $_SESSION[‘login’] != ”))
    {
    header(“Location:menupage.php”);
    }
    Here i use header(“location:menupage.php”);
    but this is not redirecting me to this page..when it is a valid user.
    just stays in the login page.

    I didnt know where to look in for…. Is there any file to be included for this header()…
    Am using eclipse for learning php..

  33. Graciela Graciela

    Vinitha:
    try this:
    session_start();
    $_SESSION[‘login’]=$uname;
    echo “session=”. $_SESSION[‘login’];
    if (!(isset($_SESSION[‘login’]) && $_SESSION[‘login’] != ”))
    {
    header(“Location:menupage.php”);
    exit;
    }

  34. zen zen

    header(“Location: Test/select.php”);

    Warning: Cannot modify header information – headers already sent by (output started at C:\wamp\www\Test\insertcheck.php:21) in C:\wamp\www\Test\insertcheck.php on line 43

    I says Why error?

  35. makis makis

    hi everyone !!!!
    when i use headers at localhost , everything is all right but when i’m on server of hebergment it doesn”t work ::!!! help please !!

    this is the line-code:

  36. Qwiz Qwiz

    I was wondering if this header statement can be done with target as well?

    say, not only redirecting this page, but targeting a frame to reload with new page as well?

    thanks in advance

  37. Venkat Venkat

    Hi Greg,
    When I am redirecting the page after deleting both pages two times the header is showing Can u help me………!!

  38. Phathuxolo Phathuxolo

    Hi Greg i have a similar problem to vinitha but the headers just dont work even if login is verified with database and is successful Ive looked everywhere for help..
    <?phpfunction Verify{
    verifying code…if success-header(“Location : home.php);
    }
    ?>

    ….<input type="submit" onclick=>

  39. kundan kundan

    thanks
    for
    Header()function

  40. M M

    If you are getting this message:

    “Cannot add/modify header information – headers already sent by…“.

    then you must enable output buffering in PHP.

    To enable output buffering put this at the very top of your HTML page before anything else:

  41. Roland Roland

    Thanks, this just saved me, quite new to php. Thanx a lot…

  42. A bit late to reply the the question about the most search engine friendly redirect but generally a 301 (permanent) redirect is best.

  43. YL YL

    i have this problem. idk if its the server problem or what. can someone help?

    i used session and header(location:…) but when i submit th form, it just refresh without redirecting!
    if i use session with echo , it does redirect BUT the session variable seem to not be there.

    what should i do? i need an answer badly! PLEASE HELP!

  44. Onur Onur

    Warning: Cannot modify header information – headers already sent by (output started at for header(‘ ‘)

  45. Felix Kusaba Felix Kusaba

    All your examples return: Warning: Cannot modify header information – headers already sent by (output started at … Why?

  46. Hi,
    The header() function works if there isn’t any output to browser before the line where it is added.

Leave a Reply

Your email address will not be published. Required fields are marked *