php取得客戶端網卡mac實體位址

WBOY
發布: 2016-07-25 09:13:28
原創
1802 人瀏覽過

php编程中,获取到用户mac地址,就可以实现与客户电脑的绑定、防止垃圾注册等。
一个非常简单的类,使用时只要实例化后直接打印它的macAddr属性即可。

代码:

  1. class Getmac{
  2. var $result = array(); // 返回带有MAC地址的字串数组
  3. var $macAddr;
  4. /*构造*/
  5. function __construct($osType){
  6. switch ( strtolower($osType) ){
  7. case "unix": break;
  8. case "solaris": break;
  9. case "aix": break;
  10. case "linux": {
  11. $this->for_linux_os();
  12.             }break;
  13.             default: {
  14.                 $this->for_windows_os();
  15.             }break;
  16.         }
  17.         $temp_array = array();
  18.         foreach($this->result as $value){
  19.             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",$value,
  20.                 $temp_array ) ){
  21.                 $this->macAddr = $temp_array[0];
  22.                 break;
  23.             }
  24.         }
  25.         unset($temp_array);
  26.         return $this->macAddr;
  27.     }
  28.     /*linux系统中获取方法*/
  29.     function for_linux_os(){
  30.         @exec("ifconfig -a", $this->result);
  31.         return $this->result;
  32.     }
  33.     /*win系统中的获取方法*/
  34.     function for_windows_os(){
  35.         @exec("ipconfig /all", $this->result);
  36.         if ( $this->result ) {
  37.             return $this->result;
  38.         } else {
  39.             $ipconfig = $_SERVER["WINDIR"]."system32ipconfig.exe";
  40.             if(is_file($ipconfig)) {
  41.                 @exec($ipconfig." /all", $this->result);
  42.             } else {
  43.                 @exec($_SERVER["WINDIR"]."systemipconfig.exe /all", $this->result);
  44.                 return $this->result;
  45.             }
  46.         }
  47.     }
  48. }
  49. /*1.实现化类   2.直接访问它的macAddr属性*/
  50. $getMac = new Getmac(PHP_OS);
  51. echo $getMac->macAddr;
  52. ?>
复制代码


客戶端, 網路卡


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!