Thursday, September 19, 2024

Redirecting a Page Using PHP

Redirecting a page in PHP is an easy task. You can use the PHP Header method to redirect a visitor to a new page. The header() method is used to send HTTP headers back to the browser. The headers can be sent back to the browser only if there’s no content or error messages shown in the page.

Here’s an example on how to redirect a visitor to http://www.newpage.com/

<?
header('Location: http://www.newpage.com/');
?>

The above example shows how to redirect the visitor to a static address. You can also dynamically create the location to which the visitor is to be redirected.

<?
&nbsp&nbsp&nbsp $domain="http://www.google.com/search?q=";
&nbsp&nbsp&nbsp $querystring="vinu%20thomas";
&nbsp&nbsp&nbsp $redirectstr= $domain.$querystring;
&nbsp&nbsp&nbsp header('Location:'. $redirectstr);
?>

The above code will create the URL for redirection as http://www.google.com/search?q=vinu%20thomas and then redirect them to the URL. You can also build php code which can conditionally redirect a page. See how this is done in the following example.

<?
&nbsp&nbsp&nbsp if($user==="Admin")
&nbsp&nbsp&nbsp {
&nbsp&nbsp&nbsp&nbsp header('Location:admin.php');
&nbsp&nbsp&nbsp }
&nbsp&nbsp&nbsp else
&nbsp&nbsp&nbsp {
&nbsp&nbsp&nbsp&nbsp header('Location:login.php');
&nbsp&nbsp&nbsp }
?>

For more information on the header() method, you can check out php.net’s header manual at: http://www.php.net/manual/en/function.header.php

Vinu Thomas is a consultant on Webdesign and Internet Technologies. His website is http://www.vinuthomas.com. You can discuss about this article or any PHP/MYSQL related issues in our Discussion Forums: http://www.Vinuthomas.com/forum2.html

Related Articles

3 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

There are a number of factors and features to consider when evaluating a new help desk software.