-1

I have a snippet of code from an existing system that is pulling the users 2-digit country code from an obsolete database table. Normally I have updated the table on a regular basis but I am quite honestly sick of doing it. What I would like to do is make this more of a live pull each time a user logs in. This is the existing code

    public static function geoip_country_code_by_name($ip)
    {
        if ($_SERVER['HTTP_CF_IPCOUNTRY']) {
            return $_SERVER['HTTP_CF_IPCOUNTRY'];
        }
        $sql = "SELECT country  FROM ip2country WHERE ip < INET_ATON('$ip') ORDER BY ip DESC LIMIT 0,1";
        $res = DB::selectOneBySQL($sql);
        return $res->country;
    }

I am looking for suggestions on how to best accomplish this.

ArcticMediaRyan
  • 687
  • 8
  • 32

1 Answers1

1

I decided to use this solution. It also allows me the option to grab additional information in the future if required. I might eventually move to a paid service either with this API or another but for now this solved my issue

         public static function geoip_country_code_by_name($ip)
        {
                if ($_SERVER['HTTP_CF_IPCOUNTRY']) {
                    return $_SERVER['HTTP_CF_IPCOUNTRY'];
                }
           $iptolocation = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
           $creatorlocation = $iptolocation->geoplugin_countryCode;
           return $creatorlocation;
}
ArcticMediaRyan
  • 687
  • 8
  • 32