ISSUE :
 
Profile photo
Submitted by Richard Cox
on June 10th, 2011 at 07:47:24 EST

How to redirect to different page in PHP?

In ASP world, there use to be Response.Redirect() to redirect to a page. Is there any similar function to redirect in PHP? I am looking for temporary page direction in PHP. This should be simple but not able to find. Please help.


Profile photo
Replied by RaKumar
on June 10th, 2011 at 09:29:13 EST
There is no Response.Redirect() concept in PHP. For page redirection in PHP, you actually need to pass HTML "Location" header information in your HTTP reponse. You can do this using PHP header() function.
Code snippet:
<?php
header('Location: http://www.codeissue.com/';);
?>

Here in this code, you are passing HTTP Header "Location: URL". However, header information should be included before any HTML content is written to the response.

If you want permanent redirection, you can pass more parameter to PHP header() function.
<?php
header('Location: http://www.codeissue.com/';, true, 301);
?>

Here, 3rd parameter is Http response code 301 which mean permanent redirection. Http response code 302 is for temporary redirection. Second parameter is boolean whether to replace any similar http header. For example, if you already have http header "Location:" then true in the second parameter will override the previous "Location:" with new one.
Accepted Answer
Accepted
 Answer

Profile photo
Replied by Richard Cox
on June 11th, 2011 at 09:57:17 EST
Thanks. I was hoping for build-in method but this works great as well.