Current location: Hot Scripts Forums » General Community » Script Requests » PHP - Converting IP to Country


PHP - Converting IP to Country

Reply
  #11 (permalink)  
Old 05-19-08, 10:40 AM
Boraan's Avatar
Boraan Boraan is offline
Coding Addict
 
Join Date: Jul 2007
Location: Clayton, NC
Posts: 292
Thanks: 0
Thanked 1 Time in 1 Post
I was hoping that you all would solve this, but no one has answered the question. He asked for a script that would do it, not information on where to find it. Is this a programmers site or what? sheesh...

Alright. To understand how the script works, you have to know something about networking. Specifically the process of mapping domain names to IP Addresses, cause that's where the magic happens.

Skipping the cool information on how your computer does lookups and how it does the domain name to IP and DNS, DHCP info we'll get to the meat of it. The IP mappings are all stored in a database by central registries. These are the ones who play a vital role in Domain Name mitigation. They are ARIN, RIPE, APNIC, LACNIC and recently IANA.

The reason why all the scripts look like they are querying other sites is because they are. They hold a copy of Internic's international registry for domain names and ip addresses. Internic doesn't allow querying beyond registrars and whois, but some of the others do.

Why would you query a domain name registry for an IP? Again, skipping the DHCP, DNS discussion, registries have a database that maps domain names, (like programmingtalk.com) to their IP address, (69.20.37.97) - <CMD: PING programmingtalk.com in case you all wanted to know how I did that.

In that database you will find alot of useful information, which we'll get to in a moment, but first, let's finish the process.

If you were going to write you own script, you first have to convert an ip address to an ip number. I'm a perl developer so...

IP Address -> IP Number
Code:
my (@octets,$octet,$ip_number,$number_convert,$ip_address);
$ip_address = $ARGV[0];
chomp ($ip_address);
@octets = split(/\./, $ip_address);
$ip_number = 0;
foreach $octet (@octets) {
$ip_number <<= 8;
$ip_number |= $octet;
}
print "\nThe IP Address $ip_address converts to the IP Number $ip_number.\n";
in PHP it would be:
Code:
$ip_number = ip2long($ip_address);
the reverse, ip number to ip address would be:
Code:
my (@octets,$i,$ip_number,$ip_number_display,$number_convert,$ip_address);
$ip_number_display = $ip_number = $ARGV[0];
chomp($ip_number);
for($i = 3; $i >= 0; $i--) {
$octets[$i] = ($ip_number & 0xFF);
$ip_number >>= 8;
}
$ip_address = join('.', @octets);
print "\nThe IP Number $ip_number_display converts to the IP Address $ip_address.\n";
in PHP:
Code:
$ip_address = long2ip($ip_number);
if you are wondering why PHP is shorter, it's because PHP has native call functions. In Perl you build your subroutines, which is why I like perl. It's a true programmer's language. With PHP you just know the native call functions and you're good to go. No programming necessary. lol

I'm going to use APNIC to retrieve the information. In perl of course, and using the WWW::CURL module.

cURL Retrival
Code:
cURL Retrieval Section
my ($ip_date, $file_time, $file, $apnic, $ripe, $arin, $lacnic, @registries, $url, $curl);
$ip_date = (strftime("%Y%m", localtime(time)))."01";
$file_time = (strftime("%Y%m%d-%H%M", localtime(time)));
$file = $file_time."_ip_data.txt";
$apnic = "ftp://ftp.apnic.net/pub/stats/apnic/apnic-latest";
$ripe = "ftp://ftp.apnic.net/pub/stats/ripe-ncc/_ripencc.latest";
$arin = "ftp://ftp.apnic.net/pub/stats/arin/arin.".$ip_date;
$lacnic = "ftp://lacnic.net/pub/stats/lacnic/lacnic.".$ip_date;
open (BODY,">>$file");
@registries=($apnic,$ripe,$arin,$lacnic);
foreach $url(@registries){
# Init the curl session
$curl= WWW::Curl::easy->new() or die "curl init failed!\n";
$curl->setopt(CURLOPT_URL, $url);
$curl->setopt(CURLOPT_FTP_USE_EPSV, '0');
$curl->setopt(CURLOPT_FILE, *BODY);
if ($curl->perform() != 0) {
print "Failed ::".$curl->errbuf."\n";
}
}
close BODY;
The information retrieved comes in a pipe-character separated format..
apnic|CN|ipv4|202.127.4.0|256|19950610|assigned is an example.

Broken down it's the REGISTRY, COUNTRY_CODE, ADDRESS_TYPE, ADDRESS, NUMBER, DATE, and RANG_TYPE.

Just a quick note about range type: it's either allocated (borrowed), or assigned (owned). ISP's make use of it all the time. It means static or dynamic ip address.

What you want is the second value or the COUNTRY_CODE. If you make a database of your own to convert the codes to actual countries, you're good to go.

Sorry PHP guys. I wouldn't dare touch this is PHP, but that's just me.

Anyway, I am going to build onto my website my own database that you all can use, free of course.
__________________
Dexter Nelson
Techdex Development & Solutions
========================
Internet Marketing For Programmers | Free Market Research in 15 Minutes or Less
My Software: Hotscripts Softpedia software.techdex.net

Last edited by Boraan; 05-19-08 at 10:54 AM. Reason: making sure the code snippets are syntaxed correctly so they work when you all use 'em
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
IP Address Search Engine Using AJAX and PHP TheJohnDoe2005 General Advertisements 0 01-18-06 08:11 PM
Display vistor IP, Browser, OS, Hostname, Country Nubier General HotScripts Site Discussion 0 09-10-05 11:19 PM
PHP multi-dimensional array sorting issue aqw PHP 2 06-24-05 11:09 PM
php and ip address spidadesign PHP 0 05-16-05 05:05 PM
Converting My/SQL 'DATETIME' to PHP... giropets PHP 1 10-20-03 08:09 PM


All times are GMT -5. The time now is 11:35 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.