Home >php教程 >php手册 >PHP获取网卡MAC地址

PHP获取网卡MAC地址

WBOY
WBOYOriginal
2016-06-21 08:53:32822browse
<ol class="dp-xml">
<li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span>    </span></span></li>
<li><span>/**    </span></li>
<li class="alt"><span>获取网卡的MAC地址原码;目前支持WIN/LINUX系统    </span></li>
<li><span>获取机器网卡的物理(MAC)地址    </span></li>
<li class="alt"><span>**/    </span></li>
<li><span>      </span></li>
<li class="alt"><span>class GetMacAddr{    </span></li>
<li><span>      </span></li>
<li class="alt">
<span>        var $</span><span class="attribute">return_array</span><span> = array(); // 返回带有MAC地址的字串数组    </span>
</li>
<li><span>        var $mac_addr;    </span></li>
<li class="alt"><span>      </span></li>
<li><span>        function GetMacAddr($os_type){    </span></li>
<li class="alt"><span>             switch ( strtolower($os_type) ){    </span></li>
<li><span>                      case "linux":    </span></li>
<li class="alt">
<span>                                $this-</span><span class="tag">></span><span>forLinux();    </span>
</li>
<li><span>                                break;    </span></li>
<li class="alt"><span>                      case "solaris":    </span></li>
<li><span>                                break;    </span></li>
<li class="alt"><span>                      case "unix":    </span></li>
<li><span>                                 break;    </span></li>
<li class="alt"><span>                       case "aix":    </span></li>
<li><span>                                 break;    </span></li>
<li class="alt"><span>                       default:    </span></li>
<li>
<span>                                 $this-</span><span class="tag">></span><span>forWindows();    </span>
</li>
<li class="alt"><span>                                 break;    </span></li>
<li><span>       </span></li>
<li class="alt"><span>              }    </span></li>
<li><span>       </span></li>
<li class="alt"><span>                  </span></li>
<li>
<span>              $</span><span class="attribute">temp_array</span><span> = array();    </span>
</li>
<li class="alt">
<span>              foreach ( $this-</span><span class="tag">></span><span>return_array as $value ){    </span>
</li>
<li><span>       </span></li>
<li class="alt"><span>                        if (    </span></li>
<li><span>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",$value,    </span></li>
<li class="alt"><span>$temp_array ) ){    </span></li>
<li>
<span>                                 $this-</span><span class="tag">></span><span class="attribute">mac_addr</span><span> = $temp_array[0];    </span>
</li>
<li class="alt"><span>                                 break;    </span></li>
<li><span>                       }    </span></li>
<li class="alt"><span>       </span></li>
<li><span>              }    </span></li>
<li class="alt"><span>              unset($temp_array);    </span></li>
<li>
<span>              return $this-</span><span class="tag">></span><span>mac_addr;    </span>
</li>
<li class="alt"><span>         }    </span></li>
<li><span>       </span></li>
<li class="alt"><span>       </span></li>
<li><span>         function forWindows(){    </span></li>
<li class="alt">
<span>              @exec("ipconfig /all", $this-</span><span class="tag">></span><span>return_array);    </span>
</li>
<li>
<span>              if ( $this-</span><span class="tag">></span><span>return_array )    </span>
</li>
<li class="alt">
<span>                       return $this-</span><span class="tag">></span><span>return_array;    </span>
</li>
<li><span>              else{    </span></li>
<li class="alt">
<span>                       $</span><span class="attribute">ipconfig</span><span> = $_SERVER["WINDIR"]."\system32\ipconfig.exe";    </span>
</li>
<li><span>                       if ( is_file($ipconfig) )    </span></li>
<li class="alt">
<span>                          @exec($ipconfig." /all", $this-</span><span class="tag">></span><span>return_array);    </span>
</li>
<li><span>                       else    </span></li>
<li class="alt">
<span>                          @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this-</span><span class="tag">></span><span>return_array);    </span>
</li>
<li>
<span>                       return $this-</span><span class="tag">></span><span>return_array;    </span>
</li>
<li class="alt"><span>              }    </span></li>
<li><span>         }    </span></li>
<li class="alt"><span>       </span></li>
<li><span>       </span></li>
<li class="alt"><span>       </span></li>
<li><span>         function forLinux(){    </span></li>
<li class="alt">
<span>              @exec("ifconfig -a", $this-</span><span class="tag">></span><span>return_array);    </span>
</li>
<li>
<span>              return $this-</span><span class="tag">></span><span>return_array;    </span>
</li>
<li class="alt"><span>         }    </span></li>
<li><span>       </span></li>
<li class="alt"><span>}    </span></li>
<li><span>//方法使用    </span></li>
<li class="alt">
<span>//$</span><span class="attribute">mac</span><span> = </span><span class="attribute-value">new</span><span> GetMacAddr(PHP_OS);    </span>
</li>
<li>
<span>//echo $mac-</span><span class="tag">></span><span>mac_addr;    </span>
</li>
<li class="alt">
<span class="tag">?></span><span>   </span>
</li>
</ol>



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