Here I am going to show you how to get users country details from user IP address.There are different types of methods are available for doing that.But I will show you two ways you can can access users country code,country name etc.

how to get users country details from user IP address
how to get users country details from user IP address

First one is :We can use API for identifieing the users details.Simply there are lots of online sites are giving for API for indentifying users country details.Amongs them http://www.geoplugin.net/ is one So here I will show you how to get the users country details through API by using the site http://www.geoplugin.net/.

Second one is : We can create table in our database then save the data into table after that we can access the users country name and country code from table by using IP address.

First Method :

 "Africa",
        "AN" => "Antarctica",
        "AS" => "Asia",
        "EU" => "Europe",
        "OC" => "Australia (Oceania)",
        "NA" => "North America",
        "SA" => "South America"
    );
    if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
        $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
        if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
            switch ($purpose) {
                case "location":
                    $output = array(
                        "city"           => @$ipdat->geoplugin_city,
                        "state"          => @$ipdat->geoplugin_regionName,
                        "country"        => @$ipdat->geoplugin_countryName,
                        "country_code"   => @$ipdat->geoplugin_countryCode,
                        "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
                        "continent_code" => @$ipdat->geoplugin_continentCode
                    );
                    break;
                case "address":
                    $address = array($ipdat->geoplugin_countryName);
                    if (@strlen($ipdat->geoplugin_regionName) >= 1)
                        $address[] = $ipdat->geoplugin_regionName;
                    if (@strlen($ipdat->geoplugin_city) >= 1)
                        $address[] = $ipdat->geoplugin_city;
                    $output = implode(", ", array_reverse($address));
                    break;
                case "city":
                    $output = @$ipdat->geoplugin_city;
                    break;
                case "state":
                    $output = @$ipdat->geoplugin_regionName;
                    break;
                case "region":
                    $output = @$ipdat->geoplugin_regionName;
                    break;
                case "country":
                    $output = @$ipdat->geoplugin_countryName;
                    break;
                case "countrycode":
                    $output = @$ipdat->geoplugin_countryCode;
                    break;
            }
        }
    }
    return $output;
}

?>
    

In the first method I have created a method “ipDetails” that will use the API from website http://www.geoplugin.net/. by providing the IP address. like http://www.geoplugin.net/json.gp?ip=” . $ip.If we put the url http://www.geoplugin.net/json.gp?ip=ip_address in the browser then this site will provide us full details of that IP.
Also we can call our custom method by the way like

$countryName = ipDetails(‘Visitor’, “Country”);
$countryCode = ipDetails(‘Visitor’, “Country Code”);
$state = ipDetails(‘Visitor’, “state”);
$city = ipDetails(‘Visitor’, “city”);
$address = ipDetails(‘Visitor’, “Address”);
$location = ipDetails(‘Visitor’, “Location”);
I we have IP address then we can get all details about that ip address through this methods also like:

$ip = ‘1003.58.15.26’;
$countryName = ipDetails($ip, “Country”);
$countryCode = ipDetails($ip, “Country Code”);
$state = ipDetails($ip, “state”);
$city = ipDetails($ip, “city”);
$address = ipDetails($ip, “Address”);
$location = ipDetails($ip, “Location”);

Also we can get ip address of user if the user visiting to site through PHP $_SERVER variable.Here I have used a method called “getIp()” that will provide the ip address of visiter of the site.


Here I can simply use the getIp() method that will give the IP address of the visiter.Then I can use that IP for getting all details of user.
like :

$ip = getIp();

then after

$countryName = ipDetails($ip, “Country”);

So finaly I will get the country name of user by using all above methods through API.

Second Method :

In this case you have to create a table which will contain country list with ranges of assigned IP-addresses.Here I have used data from MaxMind GeoIP— GeoLite Country Database (CSV file).I have used that CSV file in my table and exported with sql file you can use that sql file which is available in GitHug.

This sql file contains 6 columns: first two columns are contain IP ranges in netblock — string of dot-separated octets. Columns 3-4 are contain IP-ranges in integer format and columns 5-6 are contain country code and country name.

The sql table I have created is like below :

CREATE TABLE IF NOT EXISTS `country_with_ip` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `ip_range_start` varchar(15) DEFAULT NULL,
    `ip_range_end` varchar(15) DEFAULT NULL,
    `ip_range_start_int` int(11) DEFAULT NULL,
    `ip_range_end_int` int(11) DEFAULT NULL,
    `country_code` varchar(15) DEFAULT NULL,
    `country_name` varchar(255) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;

We can download updated CSv file from http://dev.maxmind.com/geoip/legacy/geolite/ then after upload that csv file into mysql database with name of country_with_ip.
like above getIp() method we can get the visiters IP address by simply calling that method by $ip = getIp();

And then after we can use that $ip varibale sql query purpose like below :

$query = mysql_query("SELECT 'country_name' FROM 'country_with_ip' WHERE INET_ATON('".$ip."') BETWEEN ip_range_start_int AND ip_range_end_int LIMIT 1");

We can execute that query by a simple method called getCountry($ip) like below :
$countryName = getCountry($ip);
And that method would be :

    

    

Make sure we have connected the database by the following method like :


Hope you enjoyed the post with me.So please don’t be selfish ,If get benefit then please share this post with others.

how to get users country details from user IP address
Tagged on:         

Leave a Reply

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