Redirect Visitors to URL according IP Address with PHP

This script redirects visitors to a specific url according their IP address.

This script can be used on shared hostings where you are not able to install apache or php modules. The only setting you need to enable is in php.ini

allow_url_fopen = On;

<?php

$uri = "http://freegeoip.net/json/".$_SERVER['REMOTE_ADDR'];

//country code in 2 letter format eg: GB,HU,FI,RU,RO

$countrycode = "GB";

$response = file_get_contents($uri);

$arr[] = json_decode($response);

$x = $arr[0]->country_code;

if($x == $countrycode)
header('Location: http://www.google.com');
exit;

?>

Leave Comment

Your email address will not be published. Required fields are marked *