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 country database and import it with the import script (the database is FREE and they do updates every month):
Now Download ip2country.
Extract .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 this file 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 .csv and import.php are deleted.
When you want to update the database, download .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='db_name';
$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:
Which displays:
Other public functions in the 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 it, extract all files to an empty folder, and upload them to your host overwriting all old files, after that upload .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.
For help, comments, feedback, discussion ... please join our Webmaster Forums.