Get Visitor Information by IP Address in PHP
In this article, you will learn how to get a visitor's full information, like country, state, region, timezone, latitude, longitude and so on, using the visitor's IP address using the PHP programming language.
There may be different purposes for getting the IP address of the visitor. We can easily track the visitor activity on the website and get information about the full geographical locations of the visitors. We can show or hide visitors from a specific country or region for security reasons. We can also store the visitor's information in the database to create our own analytics report.

PHP provides the simplest way to get the visitor's 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 a 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 about the IP address. In this article, we are using the '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 then decode them using the 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 a very simple code in PHP to get the IP address of the visitor and 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.0469
Longitude = -77.4903
Timezone = America/New_York
Continent Code = NA
Continent Name = North America
Timezone = America/New_York
Currency Code = USD
Related Articles
Remove duplicates from array PHPComplete HTML Form Validation in PHP
PHP remove last character from string
PHP sanitize input for MySQL
PHP random quote generator
PHP String Contains
PHP calculate percentage of total
PHP Fix: invalid argument supplied for foreach
Locking files with flock()
How to Pass an Array as URL Parameter in PHP
How to generate pdf in PHP using MySQL and MPDF Library
How to Export MySQL Table data as CSV file in PHP
Read CSV file & Import data into MySQL with PHP
Save an emoji in MySQL using PHP
Google reCAPTCHA v2 PHP example
Fetch data from database in PHP and display in PDF
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