Get Visitor Information by IP Address in PHP
In this article, you will learn how to get visitor full information, like Country, State, Region, Timezone, Latitude, Longitude and so on, using visitor's IP address in PHP.
There may be different purposes to get the IP address of the visitor. We can easily track the visitor activity on the website and know about the full geographical locations of the visitors. This is also useful for security reason, we can show or hide the visitors from particular country or region. We can also store the visitor's information in the database to make our own analytics report.

PHP provides the simplest way to get the visitor information by IP address. The $_SERVER superglobal variable is used to get all of the IP relevant information. The REMOTE_ADDR constant is used to pass as parameter to $_SERVER for getting the IP address of the current user.
$ip = $_SERVER['REMOTE_ADDR'];
Next, we will use the third API to get the full information of IP address. In this article, we are using 'geoplugin' API. The information received by the third party is not more readable. So, first convert them into a string using PHP file_get_contents() function and decode them using json_decode() function.
$ip_info = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
Complete Code: Get Visitor Information by IP Address in PHP
Here is the very simple code in PHP to get IP address of the visitor and to get full location information about the visitor -
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$ip_info = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
if($ip_info && $ip_info->geoplugin_countryName != null){
echo 'Country = '.$ip_info->geoplugin_countryName.'<br/>';
echo 'Country Code = '.$ip_info->geoplugin_countryCode.'<br/>';
echo 'City = '.$ip_info->geoplugin_city.'<br/>';
echo 'Region = '.$ip_info->geoplugin_region.'<br/>';
echo 'Latitude = '.$ip_info->geoplugin_latitude.'<br/>';
echo 'Longitude = '.$ip_info->geoplugin_longitude.'<br/>';
echo 'Timezone = '.$ip_info->geoplugin_timezone.'<br/>';
echo 'Continent Code = '.$ip_info->geoplugin_continentCode.'<br/>';
echo 'Continent Name = '.$ip_info->geoplugin_continentName.'<br/>';
echo 'Timezone = '.$ip_info->geoplugin_timezone.'<br/>';
echo 'Currency Code = '.$ip_info->geoplugin_currencyCode;
}
?>
Output of the above code -
Country Code = US
City = Ashburn
Region = Virginia
Latitude = 39.0481
Longitude = -77.4728
Timezone = America/New_York
Continent Code = NA
Continent Name = North America
Timezone = America/New_York
Currency Code = USD
Related Articles
How to add google reCAPTCHA v2 in registration form using PHPComplete HTML Form Validation in PHP
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
PHP SplFileObject Standard Library
Simple File Upload Script in PHP
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL
Php file based authentication
Simple PHP File Cache
How to get current directory, filename and code line number in PHP