How to use redirects in PHP

[2847 views]




Redirect in PHP

Redirect in PHP or any other programming language means transfer user from one page of the website to another or to forward URLs (uniform resource locator). It is mostly used to specify old traffic of a website should be transferred to another URL.

It is also used to transfer users and search engine to another URL from the original URL. There are three types of redirects:-

  • 301 (move permanently)
  • 302 (move temporarily)
  • Meta Refresh

  1. 301 (move permanently)
  2. It shows to the search engine that the page has moved permanently. It is used when you transfer your whole website to another domain.

  3. 302 (move temporarily)
  4. It is used for the temporary transfer of pages, URL or website for example when we need some maintenance on our website. It is used for a temporary basis.

  5. Meta Refresh
  6. It is used to instruct a web browser to load the current web page after a specific counter of time automatically, for example: to transfer one page to another with a specific time frame.

Ways to Redirect in PHP:

To transfer one page to another in PHP 3 basic ways are used-
Using header function
By Meta Refresh
By Javascript

  1. Using header function
  2. Header function is a built-in function in PHP and it sends an HTTP header to a client or browser. When a web page is requested server sends the header function along with some additional details such as URL date, request date, etc. It is very important to know that the header function always calls before output.
    Syntax:

    header('Location: ' +$URL );

  3. Meta Refresh
  4. It is used to instruct a web browser to load the current web page after a specific counter of time automatically.
    Syntax:

    <meta http-equiv="refresh" content="0; url=$URL">

  5. Using Javascript
  6. Syntax:

    window.location = "http://www.google.com/"

Example using Header Location:

<?php header("Location: http://www.dumysite.com/");/* This is used to redirect the browser */ echo("the Browser is successfully redirected to http://www.example_site.com/"); exit; ?>
Output

the Browser is successfully redirected to http://www.example_site.com

                 





Comments










Search Anything:

Sponsored Deals ends in



Interesting Technical Quizzes:

Search Tags