2

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 Amazon.co.uk is different to the one to buy from Amazon.com 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.

Example:

If Location = UK; print "amazon-UK-link"
If Location = US; print "amazon-US-link"
If location = None of the above; print nothing
Ben Dowling
  • 17,187
  • 8
  • 87
  • 103
TonySand
  • 21
  • 2
  • 1
    easy, ask the user. The only reliable way. –  Apr 04 '12 at 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 Apr 04 '12 at 03:00

4 Answers4

1

You can use: string geoip_country_code_by_name ( string $hostname )

Example:

<?php
$country = geoip_country_code_by_name('www.example.com');
if ($country) {
    echo 'This host is located in: ' . $country;
}
?>

Output:

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.

stewe
  • 41,820
  • 13
  • 79
  • 75
  • BTW, that won't work if you haven't setup geoip. Please see instructions on how to setup geoip: http://www.php.net/manual/en/geoip.setup.php – hodl Apr 04 '12 at 02:57
1

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 http://www.hostip.info

With your own MySQL data

1.) Retrieve the visitors' IP address:

if (getenv('HTTP_X_FORWARDED_FOR')) 
{
    $ip_address = getenv('HTTP_X_FORWARDED_FOR');
} 
else 
{
    $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 202.186.13.4 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
hohner
  • 11,498
  • 8
  • 49
  • 84
  • 1
    I believe that getenv(HTTP_X_FORWARDED_FOR) should be getenv('HTTP_X_FORWARDED_FOR') and the same for REMOTE_ADDR. Also, people usually use $_SERVER or $_ENV, though I guess there's nothing wrong with getenv(). – Corbin Apr 04 '12 at 02:59
  • Also, wow, hostip.info 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 Apr 04 '12 at 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 close – hohner Apr 04 '12 at 03:05
0

You need to use geoip for that by maxmind http://www.maxmind.com/app/ip-location or there's another option, search for an IP to Country API, there some on the web.

hodl
  • 1,420
  • 12
  • 21
0

You can use a simple API from http://www.geoplugin.net/

$xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=".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
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

Output

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

Thanks

:)

Baba
  • 94,024
  • 28
  • 166
  • 217