View Full Version : MAC Address via PHP
wk_down
January 20th, 2006, 03:34 AM
I am working on a project that requires logging the client's unique MAC address into a MySQL database. I will be using PHP to populate MySQL so if there is a script or function that allows me to gather a client's MAC, I'd appreciate any help.
wk_down
January 25th, 2006, 01:02 AM
Ok, here is what I could gather so far: (Applying to Windows machines)
<?php
/* Some code to establish connection to client */
$ipconfig = popen("ipconfig /all", "r");
$ipconfig_output = fflush($ipconfig);
$mac = strspn($ipconfig_output, '??:??:??:??:??:??');
/* Code to pass $mac into MySQL database */
?>
Is there a better function than strspn() to retrieve the MAC? I am using the ??:??:??:??:??:?? because I need ? wildcards in that format to find the MAC address string.
I am relatively new to PHP so please don't flame. I am truely trying to learn on my own but could not find much better in the php.net manual.
ZYV
January 25th, 2006, 04:08 AM
Hi!
I am not sure if I correctly understand what you need, but it seems to me that you are definitively going in the wrong direction. Do you need to log the MAC address of the site visitor to the DB or your NIC's address? You should always understand "ipconfig /all" gives you YOUR NIC's MAC, not the one of someone talking to you.
If you need the MAC by IP and you know that this IP belongs to someone currently talking to you it surely must be in the ARP cache of you box, so you need
1) Get the IP of the guy
2) Crawl the ARP cache by IP
zyv:# arp -a | grep "192.168.101.57"
? (192.168.101.57) at 00:05:5D:33:XX:FF [ether] PERM on br0
So basically you need something like this:
<?php
$dirty = `arp -a | grep "192.168.101.57"`;
// Returns: "? (192.168.101.57) at 00:05:5D:33:C8:FF [ether] PERM on br0"
ereg ("([0-9A-Fa-f]{2}(:?)){6}", $dirty, $reg);
$mac = $reg[0];
// Now go ahead with your DB stuff
// ([0-9A-Fa-f]{2}(:?)){6} basically that matches the MAC, we could me more accurate => secure, but I don't care, I trust ARP output.
?>
Hope that answers your questions.
vBulletin v3.0.5, Copyright ©2000-2008, Jelsoft Enterprises Ltd.