Free ip2country (ip to country) PHP class


Download ip2country for PHP      Support      Demo

Please update to the latest version 1.0.2 .

PHPWeby ip2country PHP class for resolving IP address(es) to country. Use it for, providing location aware content, preventing fraud, displaying the country name from where the visitor is, web analytics, or blocking users from certain countries. There are many other uses, we will leave that to your imagination.

Import script for the database is included in the zip file. First you need to download GeoLite country database and import it with the import script (the database is FREE and they do updates every month):
http://www.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip
The class uses Free GeoLite database created by MaxMind, available from http://maxmind.com. Free GeoLite database is property of MaxMind and is licensed under Maxmind's open data license.

Now Download ip2country.
Extract GeoIPCountryCSV.zip and ip2country.zip in same empty folder (example: ip2c). Do not rename any of the files extracted. In the folder should be a file import.php. Open it with your editor (ex. Wordpad), change the values marked to match your server settings and save the file.

Upload the folder and the files to your web host with your favorite FTP program. . Run from your browser http://www.example.com/ip2c/import.php (replace example.com with your web site url) and do NOT stop the loading of the page or press refresh. If it shows 'Successfully inserted data' the data is inserted successfully. If there are any errors post them at our Webmaster forums. Finally, make sure that the files GeoIPCountryWhois.csv and import.php are deleted.
When you want to update the database, download GeoIPCountryWhois.csv, run the import.php script with the same database, user name, password and table name. It will automatically update the database.

Following is example code:
<?php
include_once('ip2c/ip2country.php');
$ip2c=new ip2country();
$ip2c->mysql_host='localhost';
$ip2c->db_user='user';
$ip2c->db_pass='pass';
$ip2c->db_name='ip2country';
$ip2c->table_name='ip2c';
echo 'Your country name is '. $ip2c->get_country_name() . '<br>';
echo 'Your country code is ' . $ip2c->get_country_code();
?>
Which displays this:


The above code queries the client IP address (the current visitor on the page).
You can however set another IP address to query by adding the IP address as a string argument to either the function get_country_name or get_country_code:
echo $ip2c->get_country_name('62.162.32.44');
Which displays:
Macedonia

Other public functions in the ip2country class:
create_mysql_table() - creates table using the name stored in the variable table_name
get_client_ip() - gets the IP address of the current visitor
mysql_con() - connects to mysql server using db_user, db_pass and mysql_host and sets the current database to db_name
get_mysql_con() - gets current mysql connection
close() - closes the mysql connection created with class function mysql_con().

The current version of the script is 1.0.2. It is strongly recommended for you to update. Download ip2country for PHP, extract all files to an empty folder, and upload them to your host overwriting all old files, after that upload GeoIPCountryWhois.csv, modify import.php to match your server settings, and run import.php script to update.
This class is compatible with PHP 4 & 5, MySQL 4 & 5, and is licensed under GNU GPL license.

If you like this software please link to us!
Use this code:
<a href="http://phpweby.com/software/ip2country">ip to country</a>
For help, comments, feedback, discussion ... please join our Webmaster Forums.

tags: ip2country module for PHP, ip to country PHP class