«Ip to Country PHP»
Stop wondering where your visitors are coming from

Pure PHP 5.3+ / SQLite3 Ip to Country Resolver Script
IPv4 / IPv6 compatible

First example using IpCountryPHP

I log each unique Ip Address that hits this website and then I present them on Google's GeoChart.
This shows a pseudo-heatmap of unique ip addresses distribution per country.
(This same GeoChart is available in the Heatmap at the bottom.)

Obviously the IpCountry PHP script is used to geolocate ip addresses.
It's very easy to get an IP address location with it and use the Country Code, Name and Registry in a creative way.


Why IpCountryPHP and not competitors?

This is an easy one an I will list a set of irresistable reasons right here:

  1. It's developer ready!
    It's not just a dump of data that you need to wrap your head around just to get it working and most likely fail as IPv6 is tricky. I wrapped my head around it and you just need to write code like a boss. I did the heavy lifting so you can do the enjoyable part of programming.
  2. Object oriented (OO) squeaky clean code!
    Code is properly formatted, namespaced, object oriented, documented, clean, easy to use. No global variables, no littering the global namespace… nuttin' like that.
  3. It is pure PHP 5.3 and SQLite3.
    To connoisseurs this spells self-contained web-platform/cms/… independent. This will allow you to use it in WordPress, Drupal, Joomla and/or any custom CMS that you might have. You are not platform bound and you are not database bound. MySQL, MSSQL, PostgreSQL, … anything flies as the ipcountry database file is self contained and independent of your system. It matters not what your system's primary database is, anything goes as it's completely decouples from the IpCountryPHP.
  4. Internet-lifetime Free Updates.
    The scripts updates itself (when you choose to trigger the update process) automatically. It updates from the Internet Registries and not from my site. Hence you are in no way bound by me, bound by monthly/yearly fees or anything like this. Once you purchase, you can use it without worrying if my site will be up when you wish to update your database.
  5. IPv4/IPv6 compatible.
    It's really easy with IPv4 but IPv6 is slightly more complicated. But this script works with both. As long as you feed it a valid Ip Address, whether v4 or v6, it will deal with it. As long as it's allocated, it will find it. So you have IPv4/IPv6 support hence you are quite future-proof.
  6. Fast Seeks.
    The seeks in the database are very fast. I use 2-layers of indexes (4 on IPv6) that narrow the result sets twice and as a rule of thumb, IPv4 runs in under 1ms and IPv6 runs in under 2ms. IPv4 if a 4byte (32bit) address and IPv6 is a 16byte (128bit) address. You can't even represent it as a number in PHP (in any language with its built in data-types). Hence the database breaks it down in components to enable seeking and speed things up.
    But don't take my word for it, do a resolve ip to country test.

As you can see my friend, I don't provide reusable code, I provide freedom and power.

Why do I need to resolve Ip Addresses to Countries anyway?

I'm sure you'll find more creative ways but I'll give you a few reasons here:

  1. Track your visitors countries!
    This can give you an important metric regarding the geographical success of your websites/campaigns. Especially if you're an internation company/business and need detailed pretty stats. This also allows you to have fancy statistics and track conversions per country. You'll see where you have most/least success and mend things.
  2. Block troublesome countries!
    I'm all for freedom but sometimes I like to enforce rules. You can easily track your refund rates and see which countries generate most refunds and consider taking measures. Sometimes drastic measures are required against fraud. It's your choice.
  3. Cloak/Redirect your visitors!
    You have an awesome multilingual corporate site. And you have several country level tlds that are properly transalted. Why not detect the visitors country, redirect him by default to the right website and then allow him to select a language. With a cookie, you can prevent further forced redirections but at least, in the first visit, you'll give him a nudge in the right direction.

Is it really this easy to use?

I will shut up and let code speak for itself:

source code
// I like plain output
header('Content-Type: text/plain');
// Include ip2country first
require_once __DIR__ . '/ip2country/ip2country.inc';

// Updating the database is too easy!
$ip2country_updater = new \CA\Ip2Country_Updater();
// Test if never updated or at least 7 days elapsed (you can't update twice a day)
if($ip2country_updater->CanUpdate(7)){ // Needs a minimum number of elapsed days.
     $ip2country_updater->Update(); // Update!

// Instantiate the Ip2Country resolver
$ip2country = new \CA\Ip2Country();
// Resolve an Ip Address to Country
var_dump($country = $ip2country->ResolveAddress($_SERVER['REMOTE_ADDR']));

// List Country Codes and Names (bonus array to ease your displaying of data)

// Cleanup :)
unset($ip2country, $ip2country_updater, $country);

If I haven't convinced you already…

you're either not a developer, or you've already written this for yourself. You can't be involved in webdev, seo, sem, affiliate marketing and not figure out the need for this. Like direct EU visitors to your amazon.co.uk affiliate links and your US visitors to amazon.com and CA visitors to amazon.ca… You feelin' me?

If you're not a developer, get your developer to see this site and give you the green light to purchase this.

Will it work on your Server?

The following code will tell you if it works on your system.
I don't offer refunds if you later realise it won't work on your server.
Test first, don't regret later.

source code
// Enable error reporting and display errors.
ini_set('display_errors', true);

// PHP needs to be at least version 5.3, if not better.
if(version_compare(PHP_VERSION, '5.3', '<')){
trigger_error('IP2COUNTRY: php 5.3+ is required.', E_USER_ERROR);

// FTP or cURL is required to download the IP2Country files.

if(!extension_loaded('ftp') and !extension_loaded('curl')){
trigger_error('IP2COUNTRY: [ftp] or [curl] extension needs to be loaded.', E_USER_WARNING);
// SQLite3 is the database engine that stores and resolves IPs to Countries.
trigger_error('IP2COUNTRY: [sqlite3] extension needs to be loaded.', E_USER_WARNING);

Now that we got everything out of our way, click ยป

Next screen shows you License information, Pricing and Payment buttons.