2

I am building a website for a company who has a software that gets registered to the computer, and uses the computer name and user name of the pc. How can I retrieve those values without the user needing to fill it out?

I am using codeigniter if that helps any

JonYork
  • 1,223
  • 8
  • 31
  • 52

2 Answers2

10

You can't. All PHP will ever see of the user's machine is the IP address, from which you MAY be able to do a reverse lookup and get a hostname. But this hostname is not likely to be the actual machine's name. Ditto for the user's client-side username. A website has no business knowing how a user logged into their local machine.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Ok, what about the computer name only? And it does not have to be with php. Could Jquery or something client-side acheive this? – JonYork Mar 01 '12 at 17:36
  • 1
    No. javascript also has no access to that type of information. If it was possible, you'd see every single banner ad going "Hey JonYork, with password 1234, on your 'SpeedDemon' box. how ya doin!?" – Marc B Mar 01 '12 at 17:43
  • I see your point. So the best way/easiest way to retrieve it would be to ask the user to tell us in a form? – JonYork Mar 01 '12 at 18:19
0

If everything happens on the same local network, you could use the server machine to lookup the mac address corresponding to the IP of the visitor with something like that:

/**
 * Returns MAC-address (Ethernet 2-level in OSI model address)
 * @param string $ip Address to search MAC for. If not set, IP-address from constructor'll be used
 * @return mixed
 */
public function getMAC($ip=null)
{
    if((!$ip && !$this->sCurrentIP) || !$this->_arp_allowed())
    {
        return null;
    }
    $ip=$ip?$ip:$this->sCurrentIP;
    $rgMatches=array();
    if(PHP_OS=='WINNT')
    {
       exec("arp -a", $rgResult);
       $sMacTemplate="/[\d|A-F]{2}\-[\d|A-F]{2}\-[\d|A-F]{2}\-[\d|A-F]{2}\-[\d|A-F]{2}\-[\d|A-F]{2}/i";
       foreach($rgResult as $key=>$value)
       {
          if (strpos($value, $ip)!==FALSE)
          {
             preg_match($sMacTemplate, $value, $rgMatches);
             break;
          }
       };
    }
    else
    {
       exec("arp -a | grep $ip", $rgResult);
       if(count($rgResult))
       {
           $sMacTemplate="/[\d|A-F]{2}\:[\d|A-F]{2}\:[\d|A-F]{2}\:[\d|A-F]{2}\:[\d|A-F]{2}\:[\d|A-F]{2}/i";
           preg_match($sMacTemplate, $rgResult[0], $rgMatches);
       }
    }
    return count($rgMatches)?$rgMatches[0]:null;
}
Alex
  • 11,479
  • 6
  • 28
  • 50