| |
|
Conditional Redirection of browser or visitor in PHP |
Visitors or users can be redirected to different pages or to a common page with different messages based on different conditions. For example in a site we have different types of members with different access levels. Based on their permission level we can redirect them to different pages or directories. Some time based on the IP address of the user we can redirect them to different pages. We can break the IP address and apply our range to identify the area of the IP address. This way can display the area marked for UK to visitors coming from UK. In another case we can use one common thank you page for the members after finishing a process. If the members has changed the passwords or updated the profile or posted a message, we can show the same thank you page with different messages. We will discuss all these applications here which work on conditional redirection process. Please note that we are working on header redirection so you must take care that no data is posted to browser before the redirection command comes into effect. Otherwise we will get an error message.
Warning: Cannot modify header information - headers already sent by (output started at L:\php_files\t\test6.php:13) in L:\php_files\t\test6.php on line 14
We will start with conditional redirection which reads the status of the member and redirect them based on their permission levels. For example the permission level for a general member is 1, permission level of veterans is 2 and permission level for site admin is 3. Once the member loges in using a common login process, the permission level set for the member is collected from the table and posted to the page where the permission is to be checked and redirection is to be applied. Here we will not discuss how to get the permission level but assume that the variable $permission has one of the three values 1, 2 or 3. We will use PHP switch command to match the value of $permission and then redirect to appropriate page. Read the basic of PHP redirection page to know how it works. Here is the simple code to handle this conditional redirection.
switch ($permission)
{
case 1:
header ("Location: member-page.php"); // redirect to member page
break;
case 2:
header ("Location: veteran-page.php"); // redirect to veteran page
break;
case 3:
header ("Location: admin-page.php"); // redirect to admin page
break;
default:
header ("Location: index.php"); // redirect to main page if no permission is set
break;
}
You can see different on the value of the $permission page will be redirected to different areas. We have also set on default redirection to take care if no $permission level is set.
The header redirection can be set with some pre formatted message. Here is one case we have set a message or a variable to be passed with the redirection.
case 1:
$msg=”Welcome to the member area “
header ("Location: member-page.php?msg=$msg"); // redirect to member page
break;
This way we can redirect the browser to a different page with some message or variable. Note that it is good idea to urlencode the message string before sending to redirected page.
| |
| Subscribe |
|
Submit your email address and receive
article and product notifications. Your email is safe with us.
|
|
|
|
|
|