PHP獲取機器mac代碼

廢話很少話,直接上代碼php

<?php

class GetMac
{
    public $result = array();
    public $macAddrs = array(); //全部mac地址
    public $macAddr;            //第一個mac地址

    function __construct($OS)
    {
        $this->GetMac($OS);
    }

    function GetMac($OS)
    {
        switch (strtolower($OS)) {
            case "unix":
                break;
            case "solaris":
                break;
            case "aix":
                break;
            case "linux":
                $this->getLinux();
                break;
            default:
                $this->getWindows();
                break;
        }
        $tem = array();
        foreach ($this->result as $val) {
            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", $val, $tem)) {
                $this->macAddr = $tem[0];//多個網卡時,會返回第一個網卡的mac地址,通常夠用。
                break;
                //$this->macAddrs[] = $temp_array[0];//返回全部的mac地址
            }
        }
        unset($temp_array);
        return $this->macAddr;
    }

    //Linux系統
    function getLinux()
    {
        @exec("ifconfig -a", $this->result);
        return $this->result;
    }

    //Windows系統
    function getWindows()
    {
        @exec("ipconfig /all", $this->result);
        if ($this->result) {
            return $this->result;
        } else {
            $ipconfig = $_SERVER["WINDIR"] . "\system32\ipconfig.exe";
            if (is_file($ipconfig)) {
                @exec($ipconfig . " /all", $this->result);
            } else {
                @exec($_SERVER["WINDIR"] . "\system\ipconfig.exe /all", $this->result);
                return $this->result;
            }
        }
    }
}

$obj = new GetMac(PHP_OS);
print_r($obj->result);
echo $obj->macAddr;
//獲取客戶端
//$result=`arp -a $REMOTE_ADDR`;
//$result=`nbtstat -a $REMOTE_ADDR`;
//print_r($result);
exit;
  

 

代碼到此爲止,下面是驗證.linux

接下來咱們來驗證下,windows上執行windows

 ipconfig/all  

查看下this

 

執行php代碼查看下:3d

 

 

注意,代碼用的稍微老的語法,根據實際狀況自行修改下.unix

果真,收工,睡覺了code

相關文章
相關標籤/搜索