Page redirection In PHP



Page redirection is very important in PHP, Sometimes if you want to redirects your frontend user to another page, then it is very important to know how the page redirection works in PHP? It is very simple in PHP as well as you can also do it with the HTML. Here I am going to share some cool methods how the page redirection works in PHP.

Method 1. Page Redirection in HTML

You can redirects your page easily in HTML just put the following code line in the <header></header> HTML tag. By using the syntax below you can redirects the page to your desired page link


<header>
<META HTTP-EQUIV="Refresh" CONTENT="5; URL=the-new-page-link">
</header>

CONTENT=5, means that redirect the page after 5 seconds. If you want to redirect the page immediately you can use CONTENT=0 instead of 5 or another number.

Method 2. Page Redirection by using the PHP header() function.

So, the question arises if page redirection is so simple and it can be done by using the HTML, then why we are going to do it by using the PHP. Suppose in PHP you are going to create the dynamic pages and then you may wants to redirect your visitors to different pages according to the PHP logic, this can be done by using the header(“Location: the-new-page-link”); function.

Suppose you have developed a login system which has two types of user one is Super Admin and another is the Simple User and you wants to redirects the user according to their level.

<?php
if($userlevel == 'Super Admin')
{
header("Location: superadmin.php");
}
else{
header("Location: simpleuser.php");
}
?>

The header(); function sends a raw header data (HTTP/1.1 specification specific header) to the client web browser, which instructs the browser to redirect to the given location.

For redirection of the pages in PHP use the header() function as given below

<?php
header("Location: the-new-page-link");
?>

For a successfull page you can implement it as below :

<?php
header("Location: successfull.php");
?>

Hope this will help to someone.