Home > Backend Development > PHP Problem > How to get mac address in php

How to get mac address in php

(*-*)浩
Release: 2023-02-25 12:38:01
Original
6234 people have browsed it

Obtaining the client's mac address is a very useful function, because it is an immutable unique identifier of the client. It is useless to change the IP or clear the local records.

So to obtain the mac address, you often need to use the one-machine-one activation code when purchasing software. Each machine can only be registered once for website registration, etc.

How to get mac address in php

In the development of e-commerce systems, a common function is: users add products to the shopping cart without logging in, and the shopping cart data is automatically synchronized after logging in. .

To solve this problem, we usually use COOKIE or SESSION. In fact, you can also obtain the client's mac address as a unique identifier and store it in the database, and then match it after successful login. (Recommended learning: PHP video tutorial)

The MAC address is used to uniquely identify a network card in the network. If a device has one or more network cards, each Each network card is required and will have a unique MAC address.

Such methods are often used in the cloud in the era of big data.

The code compiled online is as follows:

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

The above is the detailed content of How to get mac address in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template