问题
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
回答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.
回答2:
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
回答3:
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.
回答4:
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
:)
来源:https://stackoverflow.com/questions/10004294/php-if-statment-based-on-geo-location