« Measuring PHP page load timeBanner and link rotation »

Redirect based on referrer or IP address Posted in PHP Tutorials, HTML/CSS Tutorials | 31 Comments
Using PHP you can easily redirect your web site visitors to a different page depending on where they come from. For example you may have 2 different web sites selling a product but only one customer care web site where people submit support tickets. Depending from which of the 2 web sites a visitor comes to that support site you may want to show them different web page.

PHP stores information about the referring URL in one of its global variables - $_SERVER. You can easily access that value using the $_SERVER['HTTP_REFERER']. Now what you have to do is to see if any of the 2 domains is in that HTTP_REFERER variable. You can do this using he preg_match function.

This is the code that you can use:

<?php
$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match("/site1.com/",$referrer)) {
header('Location: http://www.customercare.com/page-site1.html');
} elseif (preg_match("/site2.com/",$referrer)) {
header('Location: http://www.customercare.com/page-site2.html');
} else {
header('Location: http://www.customercare.com/home-page.html');
};
?>


Note, that because of using the header() function that code has to be put on the very top of your PHP page. Header redirect will throw an error if there is something sent to visitors browser before calling it.

Instead of using $_SERVER['HTTP_REFERER'] you can use $_SERVER['REMOTE_ADDR'] which carries information about visitors IP address. This way you can redirect users based on their IP address. If you detected that someone is trying to hack your web site you can use that PHP redirect to send him to a “Thank you page”

<?php
$visitor = $_SERVER['REMOTE_ADDR'];
if (preg_match("/192.168.0.1/",$visitor)) {
header('Location: http://www.yoursite.com/thank-you.html');
} else {
header('Location: http://www.yoursite.com/home-page.html');
};
?>

Do you know PHP / HTML / CSS / JS well?

Write tutorial on a topic you are good in and become a trusted PHP jabber! Share your experience with millions of other webmasters visiting our website. Contact us for more information how to become a contributor.

31 Replies to "Redirect based on referrer or IP address"

enmanuel corvo November 7, 2014 at 8:18 am | Reply

0

Here is my version of PHP redirect.

<a href="http://webtricksandtreats.com/php-page-redirect/">http://webtricksandtreats.com/php-page-redirect/</a>
enmanuel corvo November 7, 2014 at 8:18 am | Reply

0

Here is my version of PHP redirect.

http://webtricksandtreats.com/php-page-redirect/
Ionut October 17, 2012 at 3:22 pm | Reply

+3

Can you make this with GEO ip verification ?
echo '<a href="http://www.jocuricubataie.info"></a>';
Charlie Hestler September 18, 2012 at 3:35 am | Reply

+7

I like this code, but I have more than just one IP address to block. Is there a way to have several IP addresses blocked, instead of just one?
Sipylus.Net November 20, 2011 at 12:41 am | Reply

+3

I like what Asela is thinking. Is that possible?
That way I can redirect proxy sites as the visitor
switches and returns.
lakshmi November 5, 2011 at 1:55 pm | Reply

+3

Hi.....
Actually in my database 9 images are there.I want to display 1st 3(1,2,3 which are in database) bannerimages for 1st refresh of http://xxxx.com/filename.php and for 2nd refresh again 3(4,5,6 which are in database)bannerimages and for 3rd refresh again 3(7,8,9 which are in database)bannerimages .so please tell me total code,how it will come........
grahajodoh November 5, 2011 at 2:21 am | Reply

+2

thank for you tips and information..
Jan November 4, 2011 at 9:46 pm | Reply

+2

My sister\'s ex is stalking her. He is going to her business website repeatedly, now that he can\'t show up on her doorstep. How we can redirect his IP to another site using code she can insert into her site and perhaps even include a special personal \"message\" just for him? ;) I know this can be done.
bob September 26, 2011 at 10:53 am | Reply

+2

The real question is how to prevent morons to actually redirect you based on your IP address which is so freaking stupid I wish there was a word good enough to describe my rage
Waqas Yousuf March 2, 2011 at 8:28 am | Reply

+6

I need Your Help

I Made 2 different Websites on Same Domain

i want That if any Visitor from Pakistan Access my domain it Show Website 1

& if any User who is Outside Pakistan It Show Website 2


Please be polite and helpful and do not spam or offend others. We promise you will be treated the same way.

Log in your free account or if you still haven't joined you can create your free account now.

Posting tip:
if you use code in your comments please put it in these tags [php], [sql], [css], [js]
PHP code example: [php] echo date("Y-m-d"); [/php]

Thank you,
~ PHPJabbers team ~