PHP Redirect – How to Send Someone to Another URL With PHP

There can be many occasions when you need to redirect a visitor to another page. This can be accomplished quite easily using HTML although you may sometimes want to do it using PHP. There may be situations where you cannot use HTML redirection; instances such as if a user is not logged in and is trying to access content that is restricted to members.

In order to use the redirection method in PHP you must include the piece of code at the very top of the page before the server can serve up any content. Therefore if you wish to use it in a condition, make sure the condition is located at the top of your page. A sample is as follows:

<?php
IF(logged_in==TRUE){
?>
<html code goes here>
<?php
}ELSE{
HEADER( ‘LOCATION: HTTP://WWW.YOURURL.COM’ ) ;
}
?>

This method is an easy way to redirect someone if you are using a PHP web application. However if you wish to permanently redirect someone to another page it is easier to use HTTP redirect, although it can be accomplished using PHP.