首頁 > 後端開發 > php教程 > PHP取得網路卡的MAC位址原碼,支援WIN/LINUX系統

PHP取得網路卡的MAC位址原碼,支援WIN/LINUX系統

WBOY
發布: 2016-07-29 08:49:47
原創
959 人瀏覽過

<code><span><span><?php</span><span>/**
* 获取网卡的MAC地址原码;目前支持WIN/LINUX系统
* 获取机器网卡的物理(MAC)地址
*/</span><span><span>class</span><span>GetMacAddr</span>
{</span><span>var</span><span>$return_array</span> = <span>array</span>(); <span>// 返回带有MAC地址的字串数组</span><span>var</span><span>$mac_addr</span>;
    <span>public</span><span><span>function</span><span>GetMacAddr</span><span>(<span>$os_type</span>)</span>
    {</span><span>switch</span> ( strtolower(<span>$os_type</span>) )
        {
            <span>case</span><span>"linux"</span>:
                <span>$this</span>->forLinux();
            <span>break</span>;
            <span>case</span><span>"solaris"</span>:
            <span>break</span>;
            <span>case</span><span>"unix"</span>:
            <span>break</span>;
            <span>case</span><span>"aix"</span>:
            <span>break</span>;
            <span>default</span>:
                <span>$this</span>->forWindows();
            <span>break</span>;
        }
        <span>$temp_array</span> = <span>array</span>();
        <span>foreach</span> ( <span>$this</span>->return_array <span>as</span><span>$value</span> )
        {
            <span>if</span> (preg_match(<span>"/[0-9a-f][0-9a-f][:-]"</span>.<span>"[0-9a-f][0-9a-f][:-]"</span>.<span>"[0-9a-f][0-9a-f][:-]"</span>.<span>"[0-9a-f][0-9a-f][:-]"</span>.<span>"[0-9a-f][0-9a-f][:-]"</span>.<span>"[0-9a-f][0-9a-f]/i"</span>,<span>$value</span>,<span>$temp_array</span> ))
            {
                <span>$this</span>->mac_addr = <span>$temp_array</span>[<span>0</span>];
                <span>break</span>;
            }
        }
        <span>unset</span>(<span>$temp_array</span>);
        <span>return</span><span>$this</span>->mac_addr;
    }  

    <span>public</span><span><span>function</span><span>forWindows</span><span>()</span>
    {</span>
        @exec(<span>"ipconfig /all"</span>, <span>$this</span>->return_array);
        <span>if</span> ( <span>$this</span>->return_array )
            <span>return</span><span>$this</span>->return_array;
        <span>else</span>
        {
            <span>$ipconfig</span> = <span>$_SERVER</span>[<span>"WINDIR"</span>].<span>"\system32\ipconfig.exe"</span>;
            <span>if</span> ( is_file(<span>$ipconfig</span>) )
                @exec(<span>$ipconfig</span>.<span>" /all"</span>, <span>$this</span>->return_array);
            <span>else</span>
                @exec(<span>$_SERVER</span>[<span>"WINDIR"</span>].<span>"\system\ipconfig.exe /all"</span>, <span>$this</span>->return_array);
            <span>return</span><span>$this</span>->return_array;
        }
    }  

    <span>public</span><span><span>function</span><span>forLinux</span><span>()</span>
    {</span>
        @exec(<span>"ifconfig -a"</span>, <span>$this</span>->return_array);
        <span>return</span><span>$this</span>->return_array;
    }
}
<span>//方法使用</span><span>$mac</span> = <span>new</span> GetMacAddr(PHP_OS);
<span>echo</span><span>$mac</span>->mac_addr;
</span></span></code>
登入後複製
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介紹了PHP取得網路卡的MAC位址原碼,支援WIN/LINUX系統,包含了linux系統,Mac位址方面的內容,希望對PHP教學有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板