Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Probably A Far Out Question on MAC Addresses

Status
Not open for further replies.

rhnewfie

Programmer
Jun 14, 2001
267
CA
Forgive me but I know nothing about PHP but had a question. Could you use PHP somehow to request the MAC address of a users PC?
 
That's what I like, good quick answer :) Thanks!
 
i wasn't being fair. i'm happy to give you an explanation as to why, if you want.
 
Actually, it can be done. There's just not a PHP builtin that will do it.

You can use the external command [tt]arp[/tt] and process the return. Just keep in mind that you can only get the MAC address of machines on your local LAN.

The following code will work on Linux. It will definately need tweaking to work on Win32:

Code:
<?php
function GetMACAddress()
{
	$retval = FALSE;
	
	$command = '/sbin/arp -n | grep ' . $_SERVER['REMOTE_ADDR'];
	$ARPTableLine = `$command`;

	if (strstr($ARPTableLine, $_SERVER['REMOTE_ADDR']) !== FALSE)
	{
		$ARPArray = preg_split('/ +/',$ARPTableLine);

		$retval = $ARPArray[2];
	}

	return $retval;
}

$mac = GetMACAddress();

if ($mac !== FALSE)
{
	print $mac;
}
else
{
	print 'not in ARP';
}



Want the best answers? Ask the best questions! TANSTAAFL!
 
true enough. <chagrin>

perhaps i made the wrong assumptions that this was for a web service.

your code works well in windows with the following tiny changes
Code:
<?
function GetMACAddress()
{
    $retval = FALSE;
    //changed
    $command = 'arp -a '.$_SERVER['REMOTE_ADDR'];
    $ARPTableLine = `$command`;
    if (strstr($ARPTableLine, $_SERVER['REMOTE_ADDR']) !== FALSE)
    {
        $ARPArray = preg_split('/ +/',$ARPTableLine);

        //changed
        return $ARPArray[10];
    }

    return $retval;
}

$mac = GetMACAddress();

if ($mac !== FALSE)
{
    print $mac;
}
else
{
    print 'not in ARP';
}
?>

a note to the OP. if you try and test this on your own local server it might not work as the REMOTE_ADDRESS might well be the loopback address (for which no ARP entries are present).
 
Right on guys! Thanks a lot!!

Say, how come this couldn't work on a web service? I mean, you can get the IP of a callers PC so would it not be possible to get the MAC as well?
 
It will work on a web service. That's how I tested my code.

However, MAC addresses are only meaningful withing your local LAN. If a machine is connecting from outside your LAN, you will not be able to get the MAC address of the machine.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top