go top

MEGA Sale

  • Colection of 65 PHP scripts for $4.29 each

Get 65 PHP scripts in a bundle for $4.29 each!

View Offer

Redirect based on referrer or IP address

Tuesday, 6th December, 2011  /  PHP Tutorials  / HTML / CSS Tutorials  /  35 Comments
Using PHP you can easily redirect your website 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');
};
?>
Share on:

35 Comments to "Redirect based on referrer or IP address"

aaaaa

aaaaa / February 16, 2018 at 14:54 pm

I want to use header('location:http://10.19.2.1/'.$_SERVER[REQUEST_URI]); but it not working.

dexter_one

dexter_one / January 23, 2017 at 09:36 am

Very Thanks for info. Very useful.

Greetings from Mexico

roxy

roxy / December 12, 2016 at 02:35 am

Grateful
This code was working properly.
But if I want to, enter several range of IP or several IP, what should I do?

please guide me

Sasho Valkanov

Sasho Valkanov / December 13, 2016 at 17:51 pm

You can check our "How To Block An IP Address From Visiting A Website" tutorial
https://www.phpjabbers.com/how-to-block-an-ip-address-from-visiting-a-website-php75.html

Muhammad Imran

Muhammad Imran / September 19, 2015 at 06:22 am

Can I redirect other site visitor on my site at this way??

mere mehboob

mere mehboob / September 8, 2015 at 12:20 pm

how to block all ip adresses from pakistan and china

i don't want the stupid people to see my website !

Ionut

Ionut / October 17, 2012 at 15:22 pm

Can you make this with GEO ip verification ?

echo '<a href="http://www.jocuricubataie.info"></a>';

Charlie Hestler

Charlie Hestler / September 18, 2012 at 03:35 am

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

Sipylus.Net / November 20, 2011 at 00:41 am

I like what Asela is thinking. Is that possible?
That way I can redirect proxy sites as the visitor
switches and returns.

lakshmi

lakshmi / November 5, 2011 at 13:55 pm

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

grahajodoh / November 5, 2011 at 02:21 am

thank for you tips and information..

Add your comment

Captcha

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

    Log in to your account to post your comments. If you still haven't joined our community yet, 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

    Free Scripts

    Add great new functionalities to your website with our Free Scripts collection.


    Free scripts

    PHP Scripts

    Check our extensive collection of top-notch PHP Scripts that will enhance your website!


    Commercial PHP scripts