wiki:ip2c

Version 16 (modified by omry, 12 years ago) (diff)

--

IP2C

IP2C is a small library that provides IP to country resolution using a binary file.
The binary file is compiled from ip-to-country free database and takes only 460kb (the CSV takes about 3.5mb).

Supports

  • Command line
  • Java
  • PHP

Performance

IP2C works directly on a binary file, and does not load it into memory, so required memory is minimal.
PHP: My tests shows that PHP code resolves an IP in 4ms. this means you can do about 250 searches in a second.
Java: My tests show that Java code resolves an IP address in 0.0018 ms, this means you can do more than 7140 searches in a second.

Download

ip2c 1.1

Source

Browse source

Command line (requires java in path)

Usage : To resolve an IP address: java -jar ip2c.jar ip-address Output format: if not found: UNKNOWN

if found: 2C 3C NAME

Example: java -jar ip2c.jar 85.64.225.159 Outputs: IL ISR ISRAEL

To build binary file from CSV: java -jar ip2c csv_file bin_file

Java code

String ip = 85.64.225.159;
IP2Country ip2c = new IP2Country();
Country c = ip2c.getCountry(ip);
if (c == null)
{
        System.out.println("UNKNOWN");                          
}
else
{
        // will output IL ISR ISRAEL
        System.out.println(c.get2cStr() + " " + c.get3cStr() + " " + c.getName());      
}

PHP

Install the ip2c.php and the binary file in the same directroy.

<?php
require_once('ip2c.php');

$ip2c = new ip2country();
$res = $ip2c->get_country("85.64.225.159");
if ($res == false)
  echo "not found";
else
{
  $o2c = $res['id2'];
  $o3c = $res['id3'];
  $oname = $res['name'];
  echo "$o2c $o3c $oname"; // will output IL ISR ISRAEL
}
?>