Redirect to another web page using PHP

Contributor Icon Contributed by qmchenry  
Tag Icon Tagged: PHP programming  

Redirection from one web page to another is a handy tool. This recipe describes the syntax that causes the client to redirect to another URL.


The following php script redirects the user to /index.php within the same site:

<? header('Location: /index.php'); ?>

The redirection target can be any suitable URL as could be used in the href parameter of an address tag (such as http://www.tech-recipes.com/index.php). The header function sends raw header data to the client web browser and in this case sends a 302 status code which instructs the browser to redirect to the given location.

 

35 Comments -


  1. Robin Pahwa said on October 15, 2008

    check your website it has no php script

  2. Quinn McHenry said on October 15, 2008

    Thanks Robin! We recently migrated to WordPress and some of our code blocks didn’t quite make it.

  3. saurabh said on November 13, 2008

    is there any alternative of this header , so that next page is redirected without any event(not by hyperlin)

  4. linu said on December 9, 2008

    hii is i possible to send variables using this

  5. vinu said on January 14, 2009

    hi thanks for this article.
    but my problem is that header() is used for redirecting once and i want to redirct page with parameter no. of times what the solution for this?

  6. Malar said on March 12, 2009

    Hi thanks for this article.This is very useful and easily clear my doubts.Thank you……….

  7. bassline77 said on March 20, 2009

    Yeah, I’ve got the some problem, when i redirect my page this way, it gives me a “headers already sent” error. Is there an alternative?

  8. Anup pokhrel said on April 19, 2009

    I also got “Headers already sent” problem

  9. asdas said on May 30, 2009

    chutad hai tu

  10. shubham gupta said on November 7, 2009

    thanks and i solved my problems

  11. Flight said on November 12, 2009

    i want to redirect my PHP page using variable instead of fixed url
    Thanks so much

  12. Dave said on November 24, 2009

    Thanks for the php redirect page code!

  13. Anonymous said on March 24, 2010

    Thanks for sharing

  14. Form builder said on May 3, 2010

    I see there are so many ways for redirect

  15. Pratik Dangol said on May 25, 2010

    can i get code for data inserting and displaying and recently update the chandes. like facebook commet in wall, while we click publish it will insert data and display the latest updated data of database.

  16. Jackie said on June 1, 2010

    just add the url like a get variable and use $_GET to retrieve it.

  17. Adnan said on August 31, 2010

    In PHP tag, we should use the following java script
    echo “location.href=’abc.php’;”;

  18. Cleverlearncebu said on October 7, 2010

    This is 302, how about 301 redirect in PHP? some info please? :)

  19. Info said on October 9, 2010

    thanks abunch

  20. Himay said on October 9, 2010

    ok thanks, still having problems getting it to work…

  21. bosna said on December 20, 2010

    great man

  22. bosna said on December 20, 2010

    This is the easiest way i guess.. No header problem at all.. keep up the good work..

  23. Shoaib said on December 27, 2010

    java script with php waow jst working good short

  24. Gunn 'em down said on December 28, 2010

    what if javascript is disabled in a browser

  25. chintoo said on January 7, 2011

    thanks mate… i got my code working with it….

  26. Gauri said on February 18, 2011

    hey this solution is awesome and works without header errors… Thanks a lot

  27. My Offhegoes said on March 31, 2011

    Your solution working like a charm.. solve my problem..

  28. Lifemate 2 said on April 4, 2011

    you know what! I already knew how to redirect but by looking at ur code … that reminds me to the url which I had entered ! the only thing was the wrong page … anyways thanks…

  29. Sendilrrk said on April 26, 2011

    Please add ob_start() to your connection file.

  30. the raging homosexual said on July 1, 2011

    this is disgraceful trolololololololol

  31. raging homo sexual nose said on July 1, 2011

    looooooooooooooooooooooooooooooooooooooool spam

  32. Ty said on August 27, 2011

    Hi,

    What “address” do you put in the initial webpage html to call the php file?

    The php file runs and redirects when I type it into the address of IE, but I can’t seem to call the file from the initial page!

  33. Stephen said on September 19, 2011

    hi
    I need to redirect my old page to a new page, the page is made in PHP. so please tell me what is the code to redirect from one page to another page.

  34. witek said on December 1, 2011

    Location must point to absolute URL with full hostname!

  35. maged said on December 20, 2011

    thanks man you are great

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -