PHP-How to redirect to another page  screenshot

PHP-How to redirect to another page

Redirection from one page to another is one of the basic requirements of any website. Webmasters and website owners often need to redirect the current page to a different page because the Link has been changed or the current page was incorrect. Page redirection with PHP can be achieved by using the following methods:

1. Header function

2. Meta refreshes tag redirect

Header function

The Header method is considered to be the fastest method to redirect traffic from one web page to another. The main advantage of this method is that it can navigate from one location to another without the reader having to click on a link or button.

Let’s consider a scenario where a buyer clicks on a product detail page and that product is out of stock, so instead of going to the page with the product details, the buyer will be redirected to a new page with some additional details, for example "That product is out of stock but will be available again within the next 5 days. Thanks for visiting.” See the example code below.

Possible reasons that can stop redirection

  • After calling the header you do not stop the script as this might stop the redirection. Instead you have to ensure the execution of next code of that PHP file is stopped. To stop the execution of next code two functions are available in PHP that are "exit()" and "die()".
  • You don't have to write the header() code properly, but make sure you remove the extra spaces and semi-colons etc. before the header() function.
  • The header function will not work if you've used a relative URL. So make sure you've used an absolute URL. A good example of an absolute URL is
  • If you've used any HTML tags or any other data before the execution of header () function then you will get the bellow warning message.
  • Warning: Cannot modify header information

Meta refreshes tag redirect

Another way to redirect traffic to a web page using PHP is to use a Meta refresh tag redirect. This method is also very useful if you want to show a message, or some HTML, for few seconds before redirecting traffic to the next page. See the example code below.

In the above example HTTP-EQUIV is a Meta attribute that tells the browser that this is an HTTP command instead of a standard Meta tag. Further refresh the value of the HTTP-EQUIV attribute that tells the server that page needs to reload. Meta content attributes contain a number value, i.e. 5 in the above example. This means that the page will be redirected after 5 seconds. To increase the seconds we need to increase the value of the "Content" attribute.

A refresh Meta tag can also be used in the tags that allow for the automatic redirection of a page after a specified time. Check the code example below.

The refresh Meta tag method is considered as an easy method of redirection but it does have some disadvantages.

  • When the “Content“ attribute is too small, then some older versions of browsers may disable the back button.
  • Google crawlers usually ignore the page rank which has the Meta tag redirection method. This META tag is an easy way of doing a refresh or a redirect, however, it has some disadvantages.