CodeIgniter Forums
IP to country using whois protocol - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: General (https://forum.codeigniter.com/forumdisplay.php?fid=1)
+--- Forum: Lounge (https://forum.codeigniter.com/forumdisplay.php?fid=3)
+--- Thread: IP to country using whois protocol (/showthread.php?tid=69500)



IP to country using whois protocol - skunkbad - 12-04-2017

I'm playing around with some code for no particular reason. Given an IP address (IP4 or IP6), it is mostly successful at determining the country. It's not super fast like using IP2Country, but it's not terribly slow. I'm wondering if any of you have experience with this, and if it's generally regarded as unreliable, or any other negative consequences. Check it out:


PHP Code:
<?php

function _request$ip$type$server )
{
    $search $type == 'registry'
        'whois:'
        'country:';

    $f fsockopen$server43$errno$errstr);

    if( ! $f )
    {
        fclose($f);
        return FALSE;
    }

    /*$ip = $type == 'country'
        ? 'n ' . $ip
        : $ip;*/

    fwrite$f$ip "\r\n" );

    $found FALSE;
    do{
        $line fgets($f1024);

       //echo $line . '<br />';

        if( stripos$line$search ) !== FALSE )
        {
            $parts explode':'$line );
            $value trim$parts[1] );
            $found TRUE;
        }
    }
    while( ! feof($f) && $found === FALSE );

    fclose($f);
    return isset( $value )
        $value
        
FALSE;
}

function 
determine_regional_registry$ip )
{
    return _request$ip'registry''whois.iana.org' );
}

function 
determine_country$ip$server )
{
    return _request$ip'country'$server );
}

$ip '204.86.16.251';   // US
$ip '209.105.243.106'// US
$ip '123.77.194.109';  // CN
$ip '188.143.234.155'// RU
$ip '61.131.86.89';    // CN
$ip '27.151.61.241';   // CN
$ip '195.154.204.88';  // FR
$ip '188.123.126.139'// GR
$ip '110.232.248.115'// IN
$ip '45.72.0.253';     // Blank ???
$ip "103.254.205.75";  // IN

if( $server determine_regional_registry$ip ) )
{
    echo $server ' says IP address ' $ip '  is in: ';
    echo determine_country$ip$server );




RE: IP to country using whois protocol - Diego2017 - 12-07-2017

Hi
@skunkbad
Maybe you can use a extra libraries shuch as

https://github.com/4riel/Geoip-codeigniter

on github you can find soo much libraries for any things...
good luck..!!


RE: IP to country using whois protocol - DanielTheGeek - 12-09-2017

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.


RE: IP to country using whois protocol - skunkbad - 12-09-2017

(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.


RE: IP to country using whois protocol - DanielTheGeek - 12-09-2017

You're welcome man :-), looking forward to working with you sometime.


RE: IP to country using whois protocol - InsiteFX - 12-10-2017

You do know that you could just make one cookie call and store the whole json array into the cookie.