Before we get started with Get Client MAC And IP Address Using PHP, first, we have to know some general details about Mac and the IP address of the system.
What is a MAC address?
MAC stands for Media Access Control and it is also known as the physical address. Which is associated with every networking device.
It is a 48-bit physical address that is printed on NIC(Network Interface Card) and is globally unique.
What is an IP address?
An IP address is also known as Intenet Protocol and logical address provided by the ISP (Internet service provider),
Which uniquely identifies the system over the internet, but it keeps changing from time to time.
Get Client’s MAC Address Using PHP
Method-1:
1 2 3 4 5 6 7 8 9 10 11 |
<?php echo GetMACAdd(); function GetMACAdd(){ ob_start(); system('getmac'); $Content = ob_get_contents(); ob_clean(); return substr($Content, strpos($Content,'\\')-20, 17); } ?> |
Method-2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php ob_start(); // Turn on output buffering system('ipconfig /all'); //Execute external program to display output $mycom=ob_get_contents(); // Capture the output into a variable ob_clean(); // Clean (erase) the output buffer $findme = "Physical"; $pmac = strpos($mycom, $findme); // Find the position of Physical text $mac=substr($mycom,($pmac+36),17); // Get Physical Address echo $mac; ?> |
Code Highlights:
In the first method, we execute getmac
the program and get the content.
In the second method, we execute system('ipconfig /all');
to display the output.
To know more about 5 Most Useful PHP String Functions You Should Know
Get Client IP Address Using PHP
1 2 3 4 5 6 7 8 9 10 |
<?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 ?> |
In the above code, we use PHP inbuilt superglobal variable $_SERVER
which takes the user’s IP address.
I hope you understand all the points about how we can get clients’ MAC and IP addresses or clients’ machine IDs.
Please let me comment below if you are facing any issues. To know more you can check here https://www.php.net/manual/en/ref.network.php
Happy Coding..!
mac address gives me boolean instead of giving the mac address
mac address line of code return boolean instead of the mac adress value