Redirect Page Using PHP

By on Oct 31, 2014 in Interesting, PHP

Share On GoogleShare On FacebookShare On Twitter

PHP Page Redirect

Using The Header Function

As a webmaster you have most likely come across a few redirects. Sometimes you need to simply redirect to another page, rather than changing links, which could be painful if you have too many. Fortunately redirects a fairly simple. And using PHP you can redirect to anywhere very easy.

There are many ways to achieve a redirect. I will discuss the one that I always use in PHP, the header function. This method is considered to be the fastest and most reliable way to achieve a page redirect.

To use this PHP page redirect method is very easy, all you will need to do is point to the new location (the redirect) inside the header function. Take a look a the code sample bellow:

 /* Redirect browser */
header("Location: http://www.webtricksandtreats.com");

It is always good practice to include exit() after to prevent any code that follows from being executed. Also, it is very important that the is nothing above the PHP header function that will be doing the redirect. There should not be any output abve it whatsoever, else it is very likely to fail.

This is a common mistake to have empty spaces or lines above the header function. This will give you endless headaches, as you will try to figure out why a simple PHP page redirect is not working as it should, not taking into consideration an empty line above it. In addition, this code needs to run before any other code, not doing so will also result in possible errors begin thrown.

Over all, doing a page redirect using PHP should be very easy, even for beginners. But nevertheless, here is a link to the full documentation is case needed.

PHP Redirect Using The Header Function

Enmanuel Corvo
Latest posts by Enmanuel Corvo (see all)
468 ad