How to redirect using PHP

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.docplanet.org/php/php-tutorial/). 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.

If you are wondering how this works you can learn how to inspect HTTP headers using LiveHTTPHeaders Firefox extension by viewing the following video tutorial. After you learned how to inspect headers, load a script containing the redirect code above and check to see what exactly causes the browser to load the new URL.

LiveHTTPHeaders Firefox extension

Example of headers when redirecting

You can see a live example for your selves right here on docplanet.org. Our web server is configured to always add www before our domain for consistency. So if you delete 'www.' from the address of this page, which is: 'http://www.docplanet.org/php/redirect-to-another-web-page-using-php/' you would get: 'http://docplanet.org/php/redirect-to-another-web-page-using-php/'. If you try to load that address you will be redirected back to www version but you will also be able to see the redirection headers being sent out by Apache server using LiveHTTPHeader Firefox Addon. Here are the headers:

 

GET /php/redirect-to-another-web-page-using-php/ HTTP/1.1
Host: docplanet.org
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.19) Gecko/2010031218 Firefox/3.0.19
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: UTF-8,*
Keep-Alive: 300
Connection: keep-alive
 
HTTP/1.1 302 Found
Date: Thu, 15 Apr 2010 13:33:46 GMT
Server: Apache
Location: www.docplanet.org/php/redirect-to-another-web-page-using-php/
Cache-Control: max-age=172800
Expires: Sat, 17 Apr 2010 13:33:46 GMT
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 216
Keep-Alive: timeout=5, max=1000
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
www.docplanet.org/php/redirect-to-another-web-page-using-php/
 
GET /php/redirect-to-another-web-page-using-php/ HTTP/1.1
Host: www.docplanet.org
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.19) Gecko/2010031218 Firefox/3.0.19
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: UTF-8,*
Keep-Alive: 300
Connection: keep-alive
Cookie: be_typo_user=1f9cdecd5d28221391b1c2a591d5980e; PHPSESSID=c0485953e66980e700b826f8d6af7375; fe_typo_user=294ee74c2694192176bfb3da064c6d1c
If-Modified-Since: Thu, 15 Apr 2010 13:33:38 GMT
 
HTTP/1.1 200 OK
Date: Thu, 15 Apr 2010 13:33:46 GMT
Server: Apache
Set-Cookie: fe_typo_user=5025abbbd7dfe911d1048f21d4c88861; path=/
Expires: 0
Last-Modified: Thu, 15 Apr 2010 13:33:46 GMT
Cache-Control: no-cache, must-revalidate
Pragma: no-cache
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 2443
Keep-Alive: timeout=5, max=1000
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
----------------------------------------------------------
www.docplanet.org/index.php
 
GET /index.php?id=19&type=66&playerUid=38&L=0 HTTP/1.1
Host: www.docplanet.org
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.19) Gecko/2010031218 Firefox/3.0.19
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: UTF-8,*
Keep-Alive: 300
Connection: keep-alive
Cookie: be_typo_user=1f9cdecd5d28221391b1c2a591d5980e; PHPSESSID=c0485953e66980e700b826f8d6af7375; fe_typo_user=5025abbbd7dfe911d1048f21d4c88861
If-Modified-Since: Thu, 15 Apr 2010 13:33:39 GMT
 
HTTP/1.1 200 OK
Date: Thu, 15 Apr 2010 13:33:47 GMT
Server: Apache
Set-Cookie: fe_typo_user=8cf1bf0c136b65854751249c4935ee85; path=/
Expires: 0
Last-Modified: Thu, 15 Apr 2010 13:33:47 GMT
Cache-Control: no-cache, must-revalidate
Pragma: no-cache
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 266
Keep-Alive: timeout=5, max=999
Connection: Keep-Alive
Content-Type: text/xml
----------------------------------------------------------

Explanation

First thing that happens is the browser requesting the web page.
Apache then responds with a 302 redirection which is an HTTP way of saying: 'This content is not here. This is where it is: location of content'
Browser then immediately requests the content from the alternative location and you end up on a www version of the page even though that is not what you typed in!

Your thoughts

No comments

Add comment

* - required field

*




*