Welcome Guest, Not a member yet? Register   Sign In
IP to country using whois protocol
#4

(12-09-2017, 05:26 AM)DanielTheGeek Wrote: Apart from Geo Ip, there's another service from IP API that I use often, the API doesn't require any form of authentication and suites my needs perfectly.

In one of my defunct applications, I wrote a little logic that connects to the API and displays the user's country's flag, based on the user's current IP.

PHP Code:
if (empty(get_cookie('user_country')) && @fsockopen("http://www.ip-api.com/"80)) {
    
$ip_info json_decode(file_get_contents('http://www.ip-api.com/json/'.$this->input->ip_address));
        
    
setcookie("user_country"$ip_info->countrystrtotime'+10 minutes' ), "/"""""TRUE);
    
setcookie("user_country_code"$ip_info->countryCodestrtotime'+10 minutes' ), "/"""""TRUE);
    
setcookie("user_city"$ip_info->citystrtotime'+10 minutes' ), "/"""""TRUE);
    
setcookie("user_isp"$ip_info->ispstrtotime'+10 minutes' ), "/"""""TRUE);
}

$country_code = (get_cookie('user_country_code')) ? $this->db->escape_str(get_cookie('user_country_code')) : 
            
'' ;
$country = (get_cookie('user_country')) ? $this->db->escape_str(get_cookie('user_country')) : 
            
'' ;
$flag '<div src="blank.gif" style="padding-top: 5px" class="flag flag-'.strtolower($country_code).'" alt="'.$country.'" /></div>';
$data = [
 
 'flag' => $flag        
]; 

Note that, I stored the API response data as cookies so I don't call the API too much and get my server IP blacklisted.

The API is also useful for getting ISP info, so functionalities like restricting access to specific ISP's can easily be implemented.

I should probably make this into some sort of library when I've got the time.

That's a nice looking service. 150 requests per minute is nice for free. Thanks.
Reply


Messages In This Thread
IP to country using whois protocol - by skunkbad - 12-04-2017, 09:09 PM
RE: IP to country using whois protocol - by skunkbad - 12-09-2017, 02:36 PM



Theme © iAndrew 2016 - Forum software by © MyBB