Heim >Backend-Entwicklung >PHP-Tutorial >PHP获取网卡的MAC地址原码,支持WIN/LINUX系统

PHP获取网卡的MAC地址原码,支持WIN/LINUX系统

WBOY
WBOYOriginal
2016-07-29 08:49:47961Durchsuche

<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教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn