0

I need to know the MAC address of the connect clients, how can I do this in PHP?

Ali
  • 71
  • 2
  • 2
  • 9
  • first.. how did you take the mac address? and do you have a bit network background? – Bagus Tesa Jul 13 '17 at 06:02
  • You need to show us your code. – Vagabond Jul 13 '17 at 06:03
  • check this out.. https://stackoverflow.com/questions/3011878/how-to-detect-the-mac-address-of-the-user-viewing-our-website-using-php?rq=1 – Zedex7 Jul 13 '17 at 06:03
  • 3
    Possible duplicate of [How can I get the MAC and the IP address of a connected client in PHP?](https://stackoverflow.com/questions/1420381/how-can-i-get-the-mac-and-the-ip-address-of-a-connected-client-in-php) – user3942918 Jul 13 '17 at 06:06
  • the code get mac address on the local server , but when i use its on live server its not show anything – Ali Jul 13 '17 at 06:09
  • i'm using ipconfig /all ,its only get my system mac address , but i want to get user mac address when they come to my site home page , – Ali Jul 13 '17 at 06:14
  • Don't dump code in comments. Edit your post – Andreas Jul 13 '17 at 06:16

5 Answers5

4

Normally it is not possible for security issue. Because MAC address is your machine address and your server can not able to access your machine. The MAC address is not broadcast beyond the LAN the device is connected to - it never leaves the router and passes to the server.

Avishake
  • 418
  • 1
  • 5
  • 17
  • can you please suggest me somthing else to make the user unique ,i mean to say that , if the user visit to my site its unique identity store into database,and whenever they come again to my site its identity not store ,beacuse its already stored – Ali Jul 13 '17 at 10:15
  • You can make this by combination of Client IP and Client Device. Though it is not guarantee that, this will always give you correct result. But at least you can maintain maximum unique identity. – Avishake Jul 14 '17 at 10:11
1

I know this thread is a little old, but I've searched a lot of the web trying to find a solutions for the same question. After a LOT of searching the web and a LOT of solutions and codes that never worked, so I wrote the two pieces of code below.

Remember that you can only get the mac address of machines on your lan, there's no way to get the mac through browser. Hope this might help others also.

<?php
$ip_address = $_SERVER['REMOTE_ADDR'];
$mac = `arp $ip_address | cut -d " " -f4`;
echo "<br />Your Mac is: ";  
echo $mac;
?>

The first version above is the most simple I could get.

And the other below is more complex because it brings also if it's proxied, and hostname.

<?php
$http_client_ip = $_SERVER['HTTP_CLIENT_IP'];
$http_x_forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR'];
$remote_addr = $_SERVER['REMOTE_ADDR'];
$hostname = gethostname();


if (!empty($http_client_ip)) {
    $ip_address = $http_client_ip;
} else if (!empty($http_x_forwarded_for)) {
    $ip_address = $http_x_forwarded_for;
} else  {
    $ip_address = $remote_addr;
}

//$output = shell_exec('arp -n $ip_address'); 
$mac = `arp $ip_address | cut -d " " -f4`;


//echo "<br /> <br />Your hostname is: $hostname";
//echo $hostname;  
echo "<br />Your Mac is: ";  
echo $mac;
//echo "<pre>$output</pre>"

?>

Once again I just wanted to leave this here to help others in the future.

Alex
  • 397
  • 1
  • 4
  • 10
0

I dont think it's possible unless its desktop application running on your user's machine.

Fernan Vecina
  • 134
  • 1
  • 8
  • can you elaborate who can i get user mac address when they visit to my site using php – Ali Jul 13 '17 at 06:21
  • in short you can't get mac address using php, the closest information you can get is the client ip address. – Fernan Vecina Jul 13 '17 at 06:30
  • can you please tell me something else about unique visitor, i want to store my site visitors into database as a unique vsitor, and you know that ip is not unique for a user – Ali Jul 14 '17 at 04:56
0

Over the internet, YOU CAN'T. BUT if client is on local network (LAN), create a php file and put this

shell_exec("sudo $arp -an ".$_SERVER['REMOTE_ADDR']); line in. NB: Make sure to add the default apache user to the sudoers file by running sudo visudo and adding "www-data ALL=NOPASSWD /usr/bin/arp" to the bottom of the file somewhere.

Dudus
  • 23
  • 7
0

You can't, unless the clients themselves send it in the message.

That's because the MAC Address is a local address that is used for local connections only. A computer uses it to communicate with others devices in its local network (LAN), such as computers, routers, printers, etc. This also means that having the MAC Addresses of the clients is also useless for long communication, since they are not in the same network.

For communicating with devices outside the local network (WAN), an IP Address and Port number are used. The routers use the IP Address to know where to send the data, while the Port number is normally used in the final router to translate the public IP Address into a private IP Address (192.168.X.X, 172.16.X.X, 10.X.X.X), known as NAT. The Port number is also used by the destination computer to know to which program/process should send the data.

Edu
  • 1,869
  • 5
  • 27
  • 32