I have created a small php class that parses the IP2country.csv file and finds the visitor’s country using his ip address.
First download the latest ip address database from http://software77.net/geo-ip/
In right sidebar you will see Download, Select IPV4 CSV (gz). Here you can also download .zip file
Then download the file.
Place the file in the same folder where this script is.
Note:You can also change the file location of IpToCountry.csv just pase this file location to parseCsv() function.
1. include the ip2address.php
2. Create a new object of IP2Country
$ip = new IpToCountry($client_ip);
3. call the parseCSV
4. to get country name
5. to get country code
6. Just for debug purpose use
7. That’s it, enjoy.
8. If you use this class just let me know through comment.
If you find any bug please report it to me.
The class is IpToCountry and can be used the following way.
<?php require 'ip2address.php'; $client_ip = '18.104.22.168'; $ip = new IpToCountry($client_ip); $ip->parseCsv(); echo $ip; ?>
Download the zipped version from http://www.samundra.com.np/downloads/ip2address.zip
ip2address.zip contains the following files
ip2address.php- This file contains the main class sourcecode
test.php – This file contains the test code.
README.txt – How to use this class file and just replica of this article or Documentation.