How to get user IP address, OS, browser and country in PHP
  John Mwaniki /   25 Aug 2021

How to get user IP address, OS, browser and country in PHP

As a developer, you may want to collect your website users' information such as their IP addresses, their operating system, the browser, and the country from which they are accessing the site.

You may do this to track user activities for security reasons or a better user experience to serve users with content that is more personalized content for their region/country.

How to collect user IP address

It is very easy to get a user IP address in PHP using the superglobal variable $_SERVER which provides the server and environment-related information.

Below are the following ways in which you can collect the IP using the $_SERVER variable:

1. $_SERVER['REMOTE_ADDR'] - This contains the real IP address of the client. That is the most reliable value you can find from the user.

2. $_SERVER['HTTP_CLIENT_IP'] - This will fetch the IP address when the user is from shared Internet services.

3. $_SERVER['HTTP_X_FORWARDED_FOR'] - This will fetch the IP address from the user when he/she is behind the proxy.

The simplest way is just to collect the REMOTE_ADDR as below:

<?php
echo "IP address is: ".$_SERVER['REMOTE_ADDR'];

However, the user can be behind a proxy server in which case the proxy may have set the $_SERVER['HTTP_X_FORWARDED_FOR'], and in this case, the $_SERVER['REMOTE_ADDR'] fails to return the correct IP address of the user.

Use the sample code below to get the more accurate one:

<?php
if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
 $ip = $_SERVER['HTTP_CLIENT_IP'];
} 
else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} 
else {
 $ip = $_SERVER['REMOTE_ADDR'];
}
echo "IP address is: ".$ip;

 

How to get the user OS and browser

You also correct the client information in PHP through the use of the variable $_SERVER.

Simply use the code below:

<?php
echo "Client details: ".$_SERVER['HTTP_USER_AGENT'];

The above code should give the result as below:

Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0

If you just want to collect the OS name and the browser name and version, do this:

<?php
$client = $_SERVER['HTTP_USER_AGENT'];
echo "Operating system: ".explode(";",$client)[1]."
"; echo "Browser: ".end(explode(" ",$client));

It should give you the result below depending on your OS and browser:

Operating system: Ubuntu
Browser: Firefox/91.0

How to get user's country

It is possible to get the country information of the website user, using their IP address with the help of the geoplugin.net API.

Simply get the PHP curl GET request as below:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.geoplugin.net/json.gp?ip=".$ip);
curl_setopt($ch, CURLOPT_HTTPHEADER,  array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

This request will give you a JSON response as below:

<?php
{
    "geoplugin_request": "xx.xx.xx.xx",
    "geoplugin_status": 200,
    "geoplugin_delay": "2ms",
    "geoplugin_credit": "Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com.",
    "geoplugin_city": "Nairobi",
    "geoplugin_region": "Nairobi Province",
    "geoplugin_regionCode": "30",
    "geoplugin_regionName": "Nairobi Province",
    "geoplugin_areaCode": "",
    "geoplugin_dmaCode": "",
    "geoplugin_countryCode": "KE",
    "geoplugin_countryName": "Kenya",
    "geoplugin_inEU": 0,
    "geoplugin_euVATrate": false,
    "geoplugin_continentCode": "AF",
    "geoplugin_continentName": "Africa",
    "geoplugin_latitude": "-1.2841",
    "geoplugin_longitude": "36.8155",
    "geoplugin_locationAccuracyRadius": "1000",
    "geoplugin_timezone": "Africa/Nairobi",
    "geoplugin_currencyCode": "KES",
    "geoplugin_currencySymbol": "K Sh",
    "geoplugin_currencySymbol_UTF8": "K Sh",
    "geoplugin_currencyConverter": 109.4944
}

You can extract the JSON data to PHP variables for the user as shown below:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.geoplugin.net/json.gp?ip=".$ip);
curl_setopt($ch, CURLOPT_HTTPHEADER,  array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
$params = json_decode($result);
echo "Continent Name: ".$params->geoplugin_continentName."<br>";
echo "Country Name: ".$params->geoplugin_countryName."<br>";
echo "Country Code: ".$params->geoplugin_countryCode."<br>";
echo "City Name: ".$params->geoplugin_city;

The above code should print out the result as shown below:

Continent Name: Africa
Country Name: Kenya
Country Code: KE
City Name: Nairobi

Conclusion

It is possible to collect important user information on your website for specific reasons such as tracking and security reasons, customizing the site content based on their region, web browser, etc.

In this article, we have covered how to collect user IP addresses, web browsers, operating systems, and the country they are from using PHP. I hope that the article has answered your question or it has been useful to you.