Ip2Country PHP Script - Geolocate IP Address to Country Code : 5ubliminal's TellinYa

<a href="http://www.tellinya.com/art2/2/">Ip2Country PHP Script - Geolocate IP Address to Country Code : 5ubliminal's TellinYa</a>
Must Reads: Web Scraping | Link Farming | Code Snippets | SEO Freeware
Reveal More!

I have received some comments about `errors` in the script. These errors were simple warnings which should be disabled by any serious coder and were generated due to unproper mysql access setup or credentials. If you setup mysql access properly as mentioned in the Usage File it will work. I use it on site and it works. But it does require a little PHP skill to comprehend it and use it accordingly.

Changes To The Script

I made some changes to the ip2country php script class in order to help all those with no write access to directories, access needed to write the temporary files with ranges in the previous version of the ip2country script. It now runs a bit faster with only cUrl used to download ip2country IP ranges database files and no longer using ftp access for file retrieval.

I also made a few more changes listed below. Make sure you update the geolocation ip database when you get the new version as the internal table with country ip ranges has altered and is no longer compatible with old one.

  • Now uses cUrl insted of FTP access for country IP ranges download
  • Altered the internal structure of the Ip2Country Geolocation table
  • Added new functions and comments on how to use them in file
  • Moved the Ip2Country PHP Class Source Code to an external html page to make this page easier to read

About The Provided PHP Class Script

First of all, this method to geolocate an IP is free, open-source and extremely easy to port to other Web Programming languages from the PHP Script Version or the IP2Country script presented here.

Click to Download the PHP Script Usage Sample
Sample Usage For IP2Country PHP Class Script

I provide here a Ip2Country PHP Class with very simple to use methods that will allow you to translate any IP address to a Country Code. The script will create an IP database with Countries and IP ranges which will be used for locating IP addresses.

The code consists of one easy to use, understand and integrate PHP class and works combined with a mySql table in a database you need to provide the class access to. It only has few functions you need to use, but you can use the mySql database table content to your requirements and add extra required functionality to help you in your specific task of geolocation.

How the ip2country geolocation script works

The functionality concept is really easy. The top registrars have daily updated files stored containing all the IP ranges and country codes. Each country code will have at least one ip range allocated.

The files containing IP ranges are :
*Change {registry} to any of : ripe, arin, apnic, afrinic, lacnic
** ripencc is called ripe-ncc in ARIN registry.

ftp://ftp.{registry}.net/public/stats/arin/delegated-arin-latest
ftp://ftp.{registry}.net/public/stats/apnic/delegated-apnic-latest
ftp://ftp.{registry}.net/public/stats/afrinic/delegated-afrinic-latest
ftp://ftp.{registry}.net/public/stats/ripencc/delegated-ripencc-latest
ftp://ftp.{registry}.net/public/stats/lacnic/delegated-lacnic-latest

The internal format of the files is as follows :

arin|US|ipv4|69.80.224.0|4096|20060822|allocated
arin|US|ipv4|69.81.0.0|65536|20030926|allocated
arin|US|ipv4|69.82.0.0|131072|20031009|allocated
arin|US|ipv4|69.84.0.0|4096|20031022|allocated
arin|US|ipv4|69.84.32.0|4096|20031022|allocated
arin|US|ipv4|69.84.64.0|4096|20060705|allocated

You observe a pattern above.

{Registry}|{CountryCode}|{IPVersion4}|{IpAddressStart}|{IpRangeLength}|{AllocationDateYYYYMMDD}|{status}.

Final comments about IP2Country PHP class

It is very easy to integrate the ip2country plugin and use in your site and in-depth understanding of how it works is not mandatory for proper usage of this free PHP ip2country script. Only basic PHP and mySql knowledge is required to use the ip2country script. No huge ip to country csv files are needed to perform ip to country lookup and this is a free ip to country method.

16 Comments Posted By Readers :

Add your comment
#1 daftar from Indonesia web
Posted on Friday, 14 December, 2007
would you give me tutorial to use and install this script ?
#2 5ubliminal web
Posted on Friday, 14 December, 2007
No problem. I'll write something more detailed this weekend.
Subscribe to RSS or checkback to see when it's done.

PS: First of all … have you read the IP2Country usage example. I can't explain it better then that and if you can't understand it fromt here … I can't teach you to setup a database and stuff on your webserver.
#3 Amazing from India
Posted on Tuesday, 12 February, 2008
Please check your SQL query in function getRegistry. Line is

$query="SELECT `* FROM `".$this->dbTable."`

That should be

$query="SELECT * FROM `".$this->dbTable."`


Nice script, couple of warnings I fixed by changing mysql_assoc to mysql_array($res, 'MYSQL_ASSOC').

Thanks for sharing with us and making our jobs easier!
#4 5ubliminal web
Posted on Tuesday, 12 February, 2008
Thanks for the fix. The version I use works but I must have messed something when I uploaded this one. I changed the syntax problem.
As for warnings: I never have them enabled. I use functions with variable parameters and such so I can't afford to be flooded by them.

Regards.
#5 Kenan from Germany
Posted on Wednesday, 27 February, 2008
Hi,

its doesnt work on me. I have add to the last in the example script this:

$vIP = $_SERVER['REMOTE_ADDR'];
$CountryCode = $IP2C->getCountry($vIP);
$Registry = $IP2C->getRegistry($vIP); // Get the registry that has allocated this range
$AllocTimeStamp = $IP2C->getAllocDate($vIP); // Get the registry allocation date of the range

echo "Country: " . $CountryCode;
echo "";
echo "IP: " . $vIP;

but its doesnt work.

http://www.ohbee.net/ip2country.php

can you tell me what is wrong?
#6 5ubliminal web
Posted on Wednesday, 27 February, 2008
Make sure you run the $IP2C->updateDatabase() to populate the database with the ranges.
If you run PHP5 replace:
function eIp2Country
with
function __construct

Make sure you configured the IP2C sql access info properly and … try again.
#7 Kenan from Germany
Posted on Wednesday, 27 February, 2008
Thanks Web,

when i run $IP2C->updateDatabase(); the following error is come:

Processing files ...
Downloading ftp://ftp.arin.net/pub/stats/afrinic/delegated-afrinic-latest

Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in /srv/www/virtual/ohbee.net/htdocs/libs/Ip2Country.php on line 65
Processing ftp://ftp.arin.net/pub/stats/afrinic/delegated-afrinic-latest (81kb)
Parsing ftp://ftp.arin.net/pub/stats/afrinic/delegated-afrinic-latest (#1700)
#8 5ubliminal web
Posted on Wednesday, 27 February, 2008
Just add this before your code and see what happens:
error_reporting(E_ALL^(E_NOTICE|E_WARNING));

I'm not sure of your PHP setup and config so try this and see what happens.
#9 kenulu from Germany web
Posted on Thursday, 28 February, 2008
thanks web,

this error is printing:

Processing files ...
Downloading ftp://ftp.arin.net/pub/stats/afrinic/delegated-afrinic-latest
Processing ftp://ftp.arin.net/pub/stats/afrinic/delegated-afrinic-latest (81kb)
Parsing ftp://ftp.arin.net/pub/stats/afrinic/delegated-afrinic-latest (#1701)
Finished ftp://ftp.arin.net/pub/stats/afrinic/delegated-afrinic-latest
Downloading ftp://ftp.arin.net/pub/stats/lacnic/delegated-lacnic-latest
Processing ftp://ftp.arin.net/pub/stats/lacnic/delegated-lacnic-latest (158kb)
Parsing ftp://ftp.arin.net/pub/stats/lacnic/delegated-lacnic-latest (#3461)
Finished ftp://ftp.arin.net/pub/stats/lacnic/delegated-lacnic-latest
Downloading ftp://ftp.arin.net/pub/stats/apnic/delegated-apnic-latest
Processing ftp://ftp.arin.net/pub/stats/apnic/delegated-apnic-latest (849kb)
Parsing ftp://ftp.arin.net/pub/stats/apnic/delegated-apnic-latest (#17905)
Finished ftp://ftp.arin.net/pub/stats/apnic/delegated-apnic-latest
Downloading ftp://ftp.arin.net/pub/stats/arin/delegated-arin-latest
Processing ftp://ftp.arin.net/pub/stats/arin/delegated-arin-latest (2546kb)

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 35 bytes) in /srv/www/virtual/ohbee.net/htdocs/libs/Ip2Country.php on line 168
#10 5ubliminal web
Posted on Thursday, 28 February, 2008
I'm not sure about you memory on server. If allocationg 8MB is a problem I would suggest increasing the mem size in PHP.ini.
I've never seen this error as I have a lot more memory available for PHP.

Let me know if increasing memory size fixes it.
#11 Tinel Barb from Romania web
Posted on Thursday, 13 March, 2008
@kenulu may be using a free webhosting, that memory limit error is quite usual in this case. Maybe a rewrite for php.ini, if availble, will fix, or a recoding of the script wich consider the freehosting with safemode php....
The script is ingenious, the permanent update is great.
Keep up this good work, man :)
#12 5ubliminal web
Posted on Thursday, 13 March, 2008
Yeah, you're probably right. Who would use free PHP hosting nowadays:)

PS: Mersi de aprecieri. Ramain in zona;)
#13 Tinel Barb from Romania web
Posted on Friday, 14 March, 2008
You'll be amazed about how much people use freehosting :)
I use free webhosting myself and it seems is enough for now.
I have a script, an mirc script, called ip2country, which uses a txt file as a database and a very small dimension. I've tried to implement the technology in PHP but with no success, becose of the limitations of PHP in working in binary.
I'd like to finish that. Whould you like to participate? :)
#14 5ubliminal web
Posted on Friday, 14 March, 2008
Sorry dar am atatea proiecte pe cap incat nu am timp de asa ceva. Nici nu stiam ca mai exista IRC. Messenger, Skype and so on…
Ma jucam cu protocolul IRC acum vreo 6-7 ani.

Bafta.
#15 Brian from United States
Posted on Sunday, 27 April, 2008
The download page is missing :(. Please send me an email when this is available!
#16 5ubliminal web
Posted on Monday, 28 April, 2008
Sorry … seems to have been a problem with the permissions.
Try now and tell me if you still have problems.
Post Feedback 
Name *
Mail *
URL
« Anti-Spam
» URL will only go live after a review. Comments are moderated. «
5ubliminal's TellinYa.com SEM & SEO Blog © 2007 - All rights reserved unless mentioned otherwise .
Rendered On : [Thursday, 07 August, 2008 - 23:21:51 GMT]   No Ajax / Flash Used Here
" Ip2Country PHP Script - Geolocate IP Address to Country Code : 5ubliminal's TellinYa "