PHP – Get Visitors Information and IP Address
Hello, lets see how to get visitors information and IP Address using PHP
Hey learners,
In this tutorial we will learn about how to get PHP – Get Visitors Information and IP Address in PHP. So Lets get started.
This is a step by step guide. So lets get started.
Step 1: Get Visitor IP Address. There is a PHP super global variables named “$_SERVER“. This super global variables holds many information about Headers, Paths, Scripts and Protocol related information’s. By using this super global variable we will collect user IP address. We will pass “REMOTE_ADDR” attribute to this variable and store it in a variable.
$user_ip_address = $_SERVER['REMOTE_ADDR'];
Step 2: Get user details: For this purpose we will use a service called Geo Plugin. It takes a parameter (IP address) and return IP related data as JSON response. Aftre getting the the response, we will store this user data in a variable. Here json_decode will create a PHP Object for us
$user_ip_address_info = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip_address));
Step 3: Now we will access the Object data and print in output.
echo 'Country Code = '.$ip_address_info->geoplugin_countryCode.'<br/>'; echo 'Country Name = '.$ip_address_info->geoplugin_countryName.'<br/>'; echo 'City = '.$ip_address_info->geoplugin_city.'<br/>'; echo 'Region = '.$ip_address_info->geoplugin_region.'<br/>'; echo 'Latitude = '.$ip_address_info->geoplugin_latitude.'<br/>'; echo 'Longitude = '.$ip_address_info->geoplugin_longitude.'<br/>'; echo 'Time_zone = '.$ip_address_info->geoplugin_timezone.'<br/>'; echo 'Continent Code = '.$ip_address_info->geoplugin_continentCode.'<br/>'; echo 'Continent Name = '.$ip_address_info->geoplugin_continentName.'<br/>'; echo 'Currency Code = '.$ip_address_info->geoplugin_currencyCode;
Step 4: Here is the full code
<?php $user_ip_address = $_SERVER['REMOTE_ADDR']; // GETTING IP ADDRESS $user_ip_address_info = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip_address)); // CALLING THE API echo 'Country Code = '.$user_ip_address_info->geoplugin_countryCode.'<br/>'; echo 'Country Name = '.$user_ip_address_info->geoplugin_countryName.'<br/>'; echo 'City = '.$user_ip_address_info->geoplugin_city.'<br/>'; echo 'Region = '.$user_ip_address_info->geoplugin_region.'<br/>'; echo 'Latitude = '.$user_ip_address_info->geoplugin_latitude.'<br/>'; echo 'Longitude = '.$user_ip_address_info->geoplugin_longitude.'<br/>'; echo 'Time_zone = '.$user_ip_address_info->geoplugin_timezone.'<br/>'; echo 'Continent Code = '.$user_ip_address_info->geoplugin_continentCode.'<br/>'; echo 'Continent Name = '.$user_ip_address_info->geoplugin_continentName.'<br/>'; echo 'Currency Code = '.$user_ip_address_info->geoplugin_currencyCode; ?>
Step 5: Sample Final Output:
Country Code = XX Country Name = XXXXX City = XXXXX Region = XXXXXX Latitude = XX.XXXX Longitude = XX.XXXX Time_zone = XXXX/XXXX Continent Code = XX Continent Name = XXXX Currency Code = XXX
That’s it!
That’s all for today. Thank you for your concentration. See you in next one.
If you like this article, please checkout our others article as well.
Laravel:
PHP:
Our YouTube Channel
See you in next one!