-
skunkbad Senior Citizen
    
-
Posts: 1,300
Threads: 63
Joined: Oct 2014
Reputation:
86
12-04-2017, 09:09 PM
(This post was last modified: 12-04-2017, 10:41 PM by skunkbad.)
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( $server, 43, $errno, $errstr, 5 );
if( ! $f ) { fclose($f); return FALSE; }
/*$ip = $type == 'country' ? 'n ' . $ip : $ip;*/
fwrite( $f, $ip . "\r\n" );
$found = FALSE; do{ $line = fgets($f, 1024);
//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 ); }
-
Diego2017 blackhack

-
Posts: 1
Threads: 0
Joined: Dec 2017
Reputation:
0
-
DanielTheGeek CI Geek

-
Posts: 7
Threads: 4
Joined: Sep 2016
Reputation:
0
12-09-2017, 05:26 AM
(This post was last modified: 12-09-2017, 11:14 AM by DanielTheGeek.)
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->country, strtotime( '+10 minutes' ), "/", "", "", TRUE); setcookie("user_country_code", $ip_info->countryCode, strtotime( '+10 minutes' ), "/", "", "", TRUE); setcookie("user_city", $ip_info->city, strtotime( '+10 minutes' ), "/", "", "", TRUE); setcookie("user_isp", $ip_info->isp, strtotime( '+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.
-
skunkbad Senior Citizen
    
-
Posts: 1,300
Threads: 63
Joined: Oct 2014
Reputation:
86
(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->country, strtotime( '+10 minutes' ), "/", "", "", TRUE); setcookie("user_country_code", $ip_info->countryCode, strtotime( '+10 minutes' ), "/", "", "", TRUE); setcookie("user_city", $ip_info->city, strtotime( '+10 minutes' ), "/", "", "", TRUE); setcookie("user_isp", $ip_info->isp, strtotime( '+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.
-
DanielTheGeek CI Geek

-
Posts: 7
Threads: 4
Joined: Sep 2016
Reputation:
0
You're welcome man :-), looking forward to working with you sometime.
|