Home >Backend Development >PHP Tutorial >PHP obtains the original MAC address of the network card, supports WIN/LINUX system

PHP obtains the original MAC address of the network card, supports WIN/LINUX system

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 08:49:47967browse

<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); }); });

The above introduces PHP to obtain the original MAC address of the network card. It supports WIN/LINUX system, including Linux system and Mac address. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn