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.

Usually these are the requirements for a redirection

  • Based on IP address ( location ) of the visitor
  • Permission level of the visitor
  • Language setting of Visitor
  • Referrer, or from where visitor has arrived.
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.
Redirecting to HTTPS Redirect 301, 302
IP address and Geo Location of visitor Storing visitor details in MySQL table
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    soham

    27-01-2009

    I am trying to use this method, but gives me a warning that it can\'t modify header information as it has already been sent earlier. Help please? I am trying to use ob_start() and ob_end_flush(), but still doesn\'t work.
    smo

    28-01-2009

    You are sending some output before ( above this line ) the redirection is executed. Check the page source to see what is that tag or text coming to browser before the redirection command.
    vanita

    12-05-2009

    pls tell me author of php and how to maintain session login time and logout time details in database
    smo

    13-05-2009

    Rasmus Lerdorf. It is easy to store login time as while verifying the id and password you can store the time in another table along with userid. But logout is the problem. If the member is clicking the logout button then you can store them otherwise you have to assume the time and update the table. Read the Who is online script to know how this can be done.
    Samy

    27-12-2010

    Hi, I am looking something like this scrip. I want to play a first time video which is working fine, but it happens that display in every page that I move on, I want to play a different video clip once the user is selecting another link. How can use the SWITCH case, and What LOCATION I should use. I am working with Joomla CMS. Thanks

    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer