Home » Php » getting mac address of client machines – PHP

getting mac address of client machines – PHP

Posted by: admin February 23, 2020 Leave a comment

Q(Question):

Hii,
I need to know is there any way to get the mac id’s of the machines accessing ma site! I want to make a access lock using the mac id’s


<?php
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 = $_SERVER['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)
reset($valueSplitted);
foreach ($valueSplitted as $spLine) {
if (preg_match("/[0-9a-f][0-9a-f][:-]".
"[0-9a-f][0-9a-f][:-]".
"[0-9a-f][0-9a-f][:-]".
"[0-9a-f][0-9a-f][:-]".
"[0-9a-f][0-9a-f][:-]".
"[0-9a-f][0-9a-f]/i",$spLine)) {
return $spLine;
}
}
}
$ipFound = false;
}
}
return false;
}
echo returnmacaddress();
?>

I found this code by googling! But its not printing anything!! 🙁

A(Answer):

then it’s probably returning false, which does not have a print representation (check with var_dump()).

A(Answer):

@Dormilich

I did not get u!! 🙁 !i have never used var_dump()!!

A(Answer):

@pradeepjain

shame on you, that’s probably the #1 debugging function in PHP. see ref.

A(Answer):

@Dormilich

okie!!i will learn now!!thanks for the comments !!

A(Answer):

I tried to print the $location value!!! it had nothin in it!!!

A(Answer):

@pradeepjain

where did you try to print it?

A(Answer):

after this line

$location = rtrim($location);

A(Answer):

may I see the code?

my output:

// formatted through xdebug
string 'which arp' (length=9)

A(Answer):

@Dormilich

sorry for the mistake i am also getting ‘which arp’ …
i dono y he he is using ‘which arp ‘ here! i am not getting the logic in that code! so i am facing problem!

A(Answer):

hasn’t there been a description?

besides the code usage looks odd.

this is what I found somewhere else and looks way more sensible.

$mac = `ping $ip && arp -a | grep $ip`;

A(Answer):

@Dormilich

okie thanks for help!will give it a try!

A(Answer):

<?php
$ip = '192.168.2.213';
$mac = `ping $ip && arp -a | grep $ip`;
echo $mac;
?>

i wrote this code !!its printing nothing! i am getting an error in error log like

ping: icmp open socket: Permission denied when i run this code!

A(Answer):

to know what arp does go to a posix machine (linux, unix, bsd, mac), open a terminal (on linux you can also use ALT+F2), type

man arp

hit enter and read the description.

A(Answer):

@pradeepjain

that’s always the problem with shell commands, you need permission.

A(Answer):

so we cannot generally find the MAC id’s of a machine connected to net as one of the problems will be permission!

A(Answer):

do you get the same result if you try

$mac = `arp -a | grep $ip`;

?

A(Answer):

@Dormilich

yeah!!!permission error!

A(Answer):

then you can’t do anything about it (although the concept is right)

A(Answer):

is there any parameter by which we can recognize a system over internet ?

A(Answer):

recognize in what kind of way?

A(Answer):

like suppose if that system is accessing ma website its should be identified by unique thing specific to that comp! bcos we need to lock our website for a few ips’ of outside internet!

A(Answer):

what is a "comp", company, computer, competitor?

do you mean allow certain IPs or exclude certain IPs?

A(Answer):

sorry for shortcuts..i mean to say a computer /pc

A(Answer):

is there anything specific about allowed/denied computers/clients?

A(Answer):

its like the website will be accessed at only 2 places say 2 hospitals so need to lock them to those places!

A(Answer):

IS there no solution for this problem . like i am using all linux machines . cant i uniquely identify the system accessing it .something like system ID / CPU ID.

A(Answer):

why should the system ID / CPU ID be submitted? you can’t read what’s not provided.

if those computers have static IPs, you can use those.

A(Answer):

nope !! but they use laptops for accessing the site. so they dont have a static IP’s .

A(Answer):

This functionality will work on a local network, but not the internet as the MAC is hidden by the clients router.

In short, the MAC is not present in the IP packet header.

You can retrieve the client IP address via <?php echo $_SERVER[‘REMOTE_ADDR’]; ?> however this is not 100% accurate because of IP address spoofing.