Find visitor’s country using his ip address

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
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.

And then

1. include the ip2address.php

require “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
echo $ip;
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.

require 'ip2address.php';
$client_ip = '';

$ip = new IpToCountry($client_ip);
echo $ip;


Download the zipped version from 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.

Samundra Shrestha

I work as fullstack developer. Having spent more than 5 years in the web development technologies, I aspire to create an environment where we can share, learn and grow together.

6 thoughts on “Find visitor’s country using his ip address

  • June 30, 2014 at 11:35 AM

    There no code in zip file
    page not found error

    • July 4, 2014 at 8:45 AM

      I have migrated my server and have possibly broken the link. I will fix it soon and let you know. As I am currently in the middle of something and can’t fix it.


  • May 7, 2013 at 8:00 AM

    The link you provide to is giving a ‘404’ error.

  • January 3, 2013 at 10:07 AM

    Hi sumandra,

    The link you provide to is giving a ‘404’ error. Do you have an up-to-date URL where I could download it?

    Many thanks,

    • June 30, 2014 at 11:36 AM

      Yes I agree


Leave a Reply

Your email address will not be published. Required fields are marked *