wiki:ip2c

Version 38 (modified by omry, 10 years ago) (diff)

--

IP2C

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

License

IP2C is released under the terms GPL v2.

Supports

  • Command line
  • Java
  • PHP

Performance

All tests done on an Athlon XP 3200+.
PHP:

  • No cache: About 1200 searches in a second.
  • Cache: not implemented (Want to help?)

Java:

  • No cache: About 8000 searches/second.
  • Memory cache: About 180,000 searches/second.

Download

ip2c 1.4.3 (Database version : 2008.01.02)
See the changelog.

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;
boolean cache = true; // true will load the file into memory, using around 460k ram. (much faster)
IP2Country ip2c = new IP2Country(cache);
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
}
?>