Free SEO Quote
Home About Services Contact

Re-directing User Based on Their Geo-graphical Location in PHP

Ever thought it would be useful to direct users from certain countries or cities to certain geo-specific pages of your website? In this tutorial I’m going to explain how geographical I.P. location works and build a small PHP script with sample code that will direct users to a geo-specific web page based on their IP address.

How Geographical I.P. Location Technology Works

What is an IP Address?

Every user on the Internet has a unique block of numbers assigned to their computer. This block of numerical digits is known as an I.P. address or Internet Protocol address. Using these address’ computers can connect, communicate and pass packets of data to each other, similar to a telephony network that uses phone numbers.

Every IP address assigned on the internet is regulated by an organization like (in the case of U.S.) A.R.I.N. They provide your Internet Service Provider with blocks of IP address ranges. The ISP then depending on the system they use, provides users of the internet service a static I.P. address (which will never change) or a dynamic I.P. address (which will change each time you log-in to your service provider).

A List of IP Regulators and Assigners Across the Continents

How Do We Determine the Geographical Location of an IP Address?

Geo-location software is used to find the geographic location of user using the computer IP address. Usually the software will identify the users IP address, then using IP look-up find out what country, organization, and/or user the IP address has been assigned, then try to guess the users location using a database of countries and cities linked to those IP ranges. There are a number of free geo IP databases available however they vary in accuracy and require updates. A good example is the maxmind database. Sometimes this data is provided by the ISP.

You can find out the location of your own computers IP address by using the DMW technology service visit http://geoip.dmwtechnologies.com/.

Creating Geo-IP redirect script using PHP and the DMW Technologies API.

Next, lets take a look at writing a simple PHP script that will direct a user to a specific page based on their country using the DMW API. You will need the following to successfully use this source code.

- A web server running PHP
- PHP Curl installed
- DMW technologies – Free API key http://geoip.dmwtechnologies.com/

The geo location redirect script

First off we need find the I.P. address of the user accessing the page. We can use a predefined variable in PHP called $_SERVER['REMOTE_ADDR']. Once we have the user IP address we can do a look up on the DMW Technology API for the Country and city of the IP address.

//store the users IP address
 $ip = $_SERVER['REMOTE_ADDR'];
//api key
 $api_key = 'f7dd76f08d614b340d62a47a748c2fd3124521ae0e2a07878c089cfd4555d1a247cec7c0f18dcae6c157ede4849cbbf6';
//url
 $Url = 'http://geoip.dmwtechnologies.com/api.php?apikey='.$api_key.'&ip='.$ip.'';

Next we need to setup a curl function to make an API callback to the DMW technologies API, the curl code is below with curl settings variables that can be edited if required.

//curl request the IP lookup against hostIP API
 function ip_api($Url){
// is curl installed?
 if (!function_exists('curl_init')){
 die('CURL is not installed!');
 }
// create a new curl resource
 $ch = curl_init();
// set URL to download
 curl_setopt($ch, CURLOPT_URL, $Url);
// set referer:
 curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com/");
// user agent:
 curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// should curl return or print the data? true = return, false = print
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// timeout in seconds
 curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// download the given URL, and return output
 $output = curl_exec($ch);
// close the curl resource, and free system resources
 curl_close($ch);
// print output
 return $output;
 }
Then we start the script by calling our ip_api function and storing the output to a variable called xml, This will store the XML string from the DMW API.
$xml = ip_api($Url);
$obj = simplexml_load_string($xml);
$xml = new SimpleXMLElement($xml);
$country_prefix = $xml->CountryFromIp[0]->CountryName;

Now we have the country prefix of the US lets direct the user to a geo specific webpage using a simple if statement. Note this example will only work for users in the United Kingdom and United States.

if($country_prefix == 'United Kingdom') {
header('Location: http://uk.yahoo.com/');
}
elseif($country_prefix == 'United States') {
header('Location: http://www.yahoo.com/');
} else {
echo 'unable to find the geo location of this address';
}

All done! Your website can now direct users to geo-specific pages using IP to country geo location.

Geo-location to zip codes

Geo-location using zip codes (IP to Zip code). Requires a non free API key which can be purchased from DMW technologies then simply use $country_prefix = $xml->CountryFromIp[0]->AreaCode; from our XML String, You can then direct users to a specific page by changing the location i.e. United Kingdom to a relevant area code or postcode.

2 Responses to “Re-directing User Based on Their Geo-graphical Location in PHP”

Leave a Reply