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’].
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.
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.
Please Login to comment...