Ali
Ali

Reputation: 91

How to get client MAC address when visit to my website?

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

Upvotes: 1

Views: 25359

Answers (5)

Alex
Alex

Reputation: 527

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.

Upvotes: 2

Edu
Edu

Reputation: 2643

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.

Upvotes: 0

Dudus
Dudus

Reputation: 25

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.

Upvotes: 0

Avishake
Avishake

Reputation: 470

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.

Upvotes: 7

Fernan Vecina
Fernan Vecina

Reputation: 153

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

Upvotes: 1

Related Questions