Open In App

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

Last Updated : 23 Feb, 2022
Like Article

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’].

// PHP program to get IP address of client
// $IP stores the ip address of client
echo "Client's IP address is: $IP";
// Print the ip address of client


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 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.

// PHP code to get the MAC address of Server
$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 Server is: $MAC";


MAC address of Server 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.

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads