Jump to content

MAC Address via PHP


wk_down

Recommended Posts

I am working on a project for a friend where I need to pass a client's MAC address to a MySQL database. I am fairly new to PHP (I have read the W3 tutorial, some of the PHP.net manual and own O'Reilly's Learning PHP 5) and cannot find a solution. What I have so far is this:

<?php/* Some code to establish connection to client */if ($user_os == "Windows")   $ip_out = system('ipconfig /all');  $mac = strspn($ip_out, '??:??:??:??:??:??');elseif ($user_os == "Linux") {  $ip_out = shell_exec()                                /* ... and so on *//* Code to pass $mac into MySQL database */?>
Is there a better function to use than strspn()? Not sure if I can pass a variable and a wildcard mask in that function anyway. Like I said, not sure where I can go with this. Any help would be much appreciated!
Link to comment
Share on other sites

I believe I have figured it out. I don't have my entire file finished yet so I haven't tested it, but here goes:

  function operating_system() {  /* Code to determine client OS -- Throw to $client_os */    if ($client_os == "Windows") {        $output = system('ipconfig /all');        $mac_pos = strpos($output, 'Physical Address') + 36;        $client_mac = substr($output, $mac_pos, ($mac_pos + 17));         setcookie('mac', $client_mac);    }    else if ($client_os == "OS X") {        $output = system('ifconfig -a');        $mac_pos = strpos($output, 'ether') + 6;        $client_mac = substr($output, $mac_pos, ($mac_pos + 17));        setcookie('mac', $client_mac);    }    else if ($client_os == "Linux") {        $output = system('ifconfig -a');        $mac_pos = strpos($output, 'HWaddr') + 7;        $client_mac = substr($output, $mac_pos, ($mac_pos + 17));        setcookie('mac', $client_mac);      }    else { print "An error has occured."; }  }
Basically, I would run some code to establish the OS of the client and pass that to $client_os. $output recieves the dump from the ipconfig/ifconfig command. $mac_pos determines the point right before the MAC address and then the substr() function throws it to $client_mac. Sounds good to me in theory. If anyone sees something I missed, please feel free to comment.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...