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.
header ("Location: member-page.php"); // redirect to member page
header ("Location: veteran-page.php"); // redirect to veteran page
header ("Location: admin-page.php"); // redirect to admin page
header ("Location: index.php"); // redirect to main page if no permission is set
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.
$msg="Welcome to the member area";
header ("Location: member-page.php?msg=$msg"); // redirect to member page
This article is written by plus2net.com team.
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.
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.
pls tell me author of php and how to maintain session login time and logout time details in database
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.
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