How to get the MAC and IP address of a connected client in PHP?

What is a MAC address?
MAC is the abbreviation of “Media Access Control” and it is a 48-bit physical address associated with every networking device. It is printed on the NIC(Network Interface Card) and is globally unique for every networking device. MAC address is used by the data-link layer to route a data packet from source to destination.

What is an IP address?
An Internet Protocol(IP) address also known as a logical address is given by the internet service provider(ISP) which uniquely identifies a system over the network. IP address keeps on changing time to time.

How to get the IP address of the connected client in PHP: $_SERVER is a PHP superglobal variable which holds information about the header, path and script locations. Superglobal variables are the predefined variables which are always accessible. These superglobals store information in the form of associative-array, and here we are going to fetch the ‘REMOTE_ADDR’ key of the $_SERVER associative array to get the client’s IP Address. ‘REMOTE_ADDR’ returns the client’s IP address

Example 1: This example illustrates how to get Client’s IP Address using $_SERVER[‘REMOTE_ADDR’].

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to get IP address of client
$IP = $_SERVER['REMOTE_ADDR'];
  
// $IP stores the ip address of client
echo "Client's IP address is: $IP";
  
// Print the ip address of client
?>

chevron_right


Output:

Client's IP address is: ::1

Note: For an online IDE, it may show runtime error or it will not show any output because private domains don’t share their IP. For localhost, IP address is 127.0.0.1 that is a loopback address and so the client’s IP address is ::1.

How to get the MAC address of the connected client in PHP: The ‘exec()’ is a function which is used to run an external program in PHP. It returns the last line from the result of the command. To get the MAC address, pass the parameter ‘getmac’ which returns the MAC address of the client. ‘getmac’ is a CMD command to get the MAC address.

Example 2: This example get the MAC Address using exec() function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP code to get the MAC address of Client
$MAC = exec('getmac');
  
// Storing 'getmac' value in $MAC
$MAC = strtok($MAC, ' ');
  
// Updating $MAC value using strtok function, 
// strtok is used to split the string into tokens
// split character of strtok is defined as a space
// because getmac returns transport name after
// MAC address   
echo "MAC address of client is: $MAC";
?>

chevron_right


Output:

MAC address of client is: 00-20-10-2A-03-0A

Note: This code will not work on online IDE, because ‘getmac’ is a CMD command. Try running it on localhost.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.