PHP IF Statment Based On Geo Location?

Go To


Is there a way to do a PHP If statement based on the persons location?

My problem comes down to Amazon Affiliate links. The link to buy a DVD on is different to the one to buy from and I want a way to only show the correct one.

At the same time, If they aren't based in either country, then I don't want the link to show in the first place.


If Location = UK; print "amazon-UK-link"
If Location = US; print "amazon-US-link"
If location = None of the above; print nothing
2012-04-04 02:46
by TonySand
easy, ask the user. The only reliable way - NoName 2012-04-04 02:51
Dagon is right. You can use IP to location services all you want, but none are 100% accurate, and some types of proxies are undetectable (and thus the wrong IP being checked). Doesn't mean location services aren't worth a try though I suppose - Corbin 2012-04-04 03:00


You can use: string geoip_country_code_by_name ( string $hostname )


$country = geoip_country_code_by_name('');
if ($country) {
    echo 'This host is located in: ' . $country;


This host is located in: US

For your case you can use: geoip_country_code_by_name($_SERVER['REMOTE_ADDR']); to get the country code for the current user.

2012-04-04 02:53
by stewe
BTW, that won't work if you haven't setup geoip. Please see instructions on how to setup geoip: - Mar Cejas 2012-04-04 02:57


You're going to have to use the visitor's IP address to lookup their physical location. Apart from using the GeoIP extension for PHP (as stewe pointed out), there are two ways of doing this:

The easy way

Use an external service like

With your own MySQL data

1.) Retrieve the visitors' IP address:

if (getenv('HTTP_X_FORWARDED_FOR')) 
    $ip_address = getenv('HTTP_X_FORWARDED_FOR');
    $ip_address = getenv('REMOTE_ADDR');

2.) Convert the visitor's IP address to an IP Number:

$ips = explode(".",$ip_address);
return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);

3.) Locate the IP Number from your database which you can download here. For example: the IP Address converts to IP Number 3401190660. It is between the beginning and the ending of the following IP numbers:

Beginning_IP | End_IP      | Country  | ISO
3401056256   | 3401400319  | MALAYSIA | MY
2012-04-04 02:55
by hohner
I believe that getenv(HTTPXFORWARDEDFOR) should be getenv('HTTPXFORWARDEDFOR') and the same for REMOTEADDR. Also, people usually use $SERVER or $_ENV, though I guess there's nothing wrong with getenv() - Corbin 2012-04-04 02:59
Also, wow, thinks my IP is in California. I'm actually in Arkansas. Just checked a few other services, and they all get it correct. (MaxMind gets it to about 3 miles >.< - Corbin 2012-04-04 03:03
@Corbin apparently I live in North England on the bank of a river. I actually in South West England in the middle of the countryside. So clos - hohner 2012-04-04 03:05


You need to use geoip for that by maxmind or there's another option, search for an IP to Country API, there some on the web.

2012-04-04 02:53
by Mar Cejas


You can use a simple API from

$xml = simplexml_load_file("".getRealIpAddr());
echo $xml->geoplugin_countryName ;

echo "<pre>" ;

foreach ($xml as $key => $value)
    echo $key , "= " , $value ,  " \n" ;

function getRealIpAddr()
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    return $ip;


United States
geoplugin_city= San Antonio
geoplugin_region= TX
geoplugin_areaCode= 210
geoplugin_dmaCode= 641
geoplugin_countryCode= US
geoplugin_countryName= United States
geoplugin_continentCode= NA
geoplugin_latitude= 29.488899230957
geoplugin_longitude= -98.398696899414
geoplugin_regionCode= TX
geoplugin_regionName= Texas
geoplugin_currencyCode= USD
geoplugin_currencySymbol= $
geoplugin_currencyConverter= 1

It makes you have so many options you can play around with



2012-04-04 12:20
by Baba