Script PHP para extraer la IP pública desde el navegador

PHP script to extract public IP from browser

This mini application is a PHP Script to extract the public IP from the browser, in addition you will be able to extract the local IP, the country, department and city from where you are connecting.

Extract public IP

To extract the public IP from the client with which the users access, the api from the website, http://checkip.amazonaws.com/, the IP is accessed with the PHP function file_get_contents to extract the text string of the IP, this IP is saved in the variable $ip_address

Extract IP from LAN network

To extract the local IP, the LAN network IP, the function getHostByName(php_uname('n')) I immediately print the extracted value with the echo command

Know the browser from where you are accessing

This process can be done with the command $_SERVER[‘HTTP_USER_AGENT’] which in my case shows me the following if I print it from PHP with an echo:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36

Basically what is done is to compare the USER AGENT, with their keywords, for example for Chrome, it is validated with the following section of code:

if (strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false)
        return 'Navegador: Google Chrome';

PHP script to know the country, department and city of the end user from the browser

To validate the user's location, an api is used that you can verify at the following link:

https://ipinfo.io/

You can extract the location, city, department, country, among other data, putting together the URL by adding a slash and the public IP from where the connection is made, plus another slash and the word geo, as follows, for example:

http://ipinfo.io/191.98.43.21/geo

What you can do is use the function file_get_contents to extract the information in a json and assign variables to each value from PHP with the following section of code:

$PublicIP = "191.95.53.31";
//$PublicIP = $ip_address;

$json     = file_get_contents("http://ipinfo.io/$PublicIP/geo");
//Breaks down the JSON object into an array
$json     = json_decode($json, true);
//This variable is the visitor's county
$country  = $json['country'];
//This variable is the visitor's region
$region   = $json['region'];
//This variable is the visitor's city
$city     = $json['city'];


echo '</br> País: ' . $country;
echo '</br> Departamento: ' . $region;
echo '</br> Ciudad: ' . $city;

Tuning of the PHP Script to extract the public IP from the browser on a local server

Previous Applications

To install the application on a local server, I recommend the prior installation of the following applications:

XAMPP, Git, Visual Studio Code

XAMPP is the application in charge of implementing a apache server for them to work scripts PHP on a local computer.

Git is the world's most widely used repository manager, with which you can cleanly download code from GitHub.

Visual Studio Code is the world's most popular code editor, which I highly recommend in virtually every post on this blog.

Download GitHub App

The following is the GitHub repository download link for the application that allows you to compress files and generate a ZIP on a local server:

Application configuration of the PHP script for its operation on your computer

You must install the mentioned applications, with all the default parameters, after this procedure, from Visual Studio Code, you must open the following location:

C:\xampp\htdocs

From this location in Visual Studio Code, click View, Terminal. Inside the Terminal you choose the Git Bash option and copy the following code:

git clone https://github.com/configuroweb/extraer-ip-publica.git

The zip folder will be created, inside the htdocs folder

Next, open XAMPP on your computer, activate the Apache service, and you will be able to see the application at the following URL:

http://localhost/extraer-ip-publica/

Upload of the PHP script to extract the public IP from the browser

Due to the simplicity of the application, I will not address the issue directly, in the following related video (spanish), I explain the process at a general level:

Upload page with PHP + Database (MYSQL) to a Hosting

Any questions I remain pending, if you require more PHP applications, in Python or JavaScript I recommend you subscribe to my content on Youtube and be on the lookout for any changes, that I make.

Leave a Reply

Your email address will not be published.