PHP IF Statment Based On Geo Location?

前端 未结 4 1470
忘掉有多难
忘掉有多难 2021-01-07 01:43

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 diff

相关标签:
4条回答
  • 2021-01-07 02:04

    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.

    0 讨论(0)
  • 2021-01-07 02:11

    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

    :)

    0 讨论(0)
  • 2021-01-07 02:12

    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
    
    0 讨论(0)
  • 2021-01-07 02:23

    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.

    0 讨论(0)
提交回复
热议问题