首页 > 开发 > Php > 正文

php获取网卡地址

2020-09-12 14:44:15
字体:
来源:转载
供稿:网友

 

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

 

挺好用的!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表