Bookmark and Share
Check Google Page Rank Locations of visitors to this page
Click to get Free offers

Thursday, September 18, 2008

Find MAC address of user by PHP

That's a small function that basically parses the arp table and returns the mac address of the client.

Be sure to understand the basics of arp, since this script will work only on a linear network, without gateways;
i.e.: to be sure that i'm getting the exact mac address, the client should be connected DIRECTLY to the php server; there can be as many switches and hubs you want, but no natting gateways, no routing should be made

thanks to Marcus [] for some hacks on this code

function returnmacaddress() {
// This code is under the GNU Public Licence
// Written by michael_stankiewicz {don't spam} at yahoo {no spam} dot com
// Tested only on linux, please report bugs

// WARNING: the commands 'which' and 'arp' should be executable
// by the apache user; on most linux boxes the default configuration
// should work fine

// get the arp executable path
$location = `which arp`;
$location = rtrim($location);
// Execute the arp command and store the output in $arpTable
$arpTable = `$location -n`;
// Split the output so every line is an entry of the $arpSplitted array
$arpSplitted = split("\n",$arpTable);
// get the remote ip address (the ip address of the client, the browser)
$remoteIp = $GLOBALS['REMOTE_ADDR'];
$remoteIp = str_replace(".", "\\.", $remoteIp);
// Cicle the array to find the match with the remote ip address
foreach ($arpSplitted as $value) {
// Split every arp line, this is done in case the format of the arp
// command output is a bit different than expected
$valueSplitted = split(" ",$value);
foreach ($valueSplitted as $spLine) {
if (preg_match("/$remoteIp/",$spLine)) {
$ipFound = true;
// The ip address has been found, now rescan all the string
// to get the mac address
if ($ipFound) {
// Rescan all the string, in case the mac address, in the string
// returned by arp, comes before the ip address
// (you know, Murphy's laws)
foreach ($valueSplitted as $spLine) {
if (preg_match("/[0-9a-f][0-9a-f][:-]".
"[0-9a-f][0-9a-f]/i",$spLine)) {
return $spLine;
$ipFound = false;
return false;

source courtsy:
View blog reactions

No comments: