【问题标题】:How to get MAC address of client using PHP?如何使用 PHP 获取客户端的 MAC 地址?
【发布时间】:2011-07-01 17:48:37
【问题描述】:

如何使用 PHP 或 javascript 获取 MAC 地址...

【问题讨论】:

  • 你不能那样做。
  • 您应该将其发布为答案。
  • 为什么会被否决?我也在寻找这个答案。
  • 然后投票。当 a.) 他们认为答案很明显或 b.) 他们认为问题的答案是“这是不可能的”或 c.) 他们想不出一个好的理由时,有些人会有一种不可抗拒的冲动来否决问题提问者想要做什么。
  • @Pointy 提问者可能不知道 MAC 地址和 IP 地址之间的区别。但我认为人们不应该担心提问者这个人,而应该更多地关注问题所问的问题。人们在互联网上搜索并找到这个问题可能有各种各样的理由来问这个确切的问题。即使提问者的理由不好,他们的理由也可能是。建议/警告可以进入 cmets。 Asker 可能意识到他毕竟不想这样做。但是其他人会发现问题和答案很有用,就像我一样。

标签: php ip


【解决方案1】:

MAC 地址(低级本地网络接口地址)无法通过 IP 路由器的跃点存活。您无法从远程服务器上找到客户端 MAC 地址。

在本地子网中,MAC 地址通过 ARP 系统映射到 IP 地址。本地网络上的接口知道如何将 IP 地址映射到 MAC 地址。但是,当您的数据包已在本地子网上路由到(并通过)网关到“真实”互联网时,原始 MAC 地址会丢失。简单地说,您的数据包的每个子网到子网跃点都涉及相同类型的 IP 到 MAC 映射,用于每个子网中的本地路由。

【讨论】:

    【解决方案2】:
    echo GetMAC();
    
    function GetMAC(){
        ob_start();
        system('getmac');
        $Content = ob_get_contents();
        ob_clean();
        return substr($Content, strpos($Content,'\\')-20, 17);
    }
    

    上面将基本上执行getmac 程序并解析其控制台输出,从而得到服务器的MAC 地址(和/或安装和运行PHP 的位置)。

    【讨论】:

    • 虽然这段代码 sn-p 可以解决问题,including an explanation 确实有助于提高您的帖子质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性 cmets 挤满你的代码,因为这会降低代码和解释的可读性!
    • 它在本地主机服务器上工作。无法在实时服务器上运行
    • @Top-Master:我删除了您的编辑,因为它具有误导性。 PHP 是服务器端语言,不能在客户端运行。请看看这个Client side vs Server side
    【解决方案3】:

    这是一种可能的方法:

    $string=exec('getmac');
    $mac=substr($string, 0, 17); 
    echo $mac;
    

    【讨论】:

    • 这在 Linux 系统中不起作用,即使它起作用,它也会为您获取正在执行脚本的服务器的 Mac 地址,因此没有客户端 Mac 地址
    【解决方案4】:

    使用该函数获取客户端MAC地址:

    function GetClientMac(){
        $macAddr=false;
        $arp=`arp -n`;
        $lines=explode("\n", $arp);
    
        foreach($lines as $line){
            $cols=preg_split('/\s+/', trim($line));
    
            if ($cols[0]==$_SERVER['REMOTE_ADDR']){
                $macAddr=$cols[2];
            }
        }
    
        return $macAddr;
    }
    

    【讨论】:

      【解决方案5】:

      如果客户端运行 Windows 并允许您安装 ActiveX 控件,您可以在 javascript 中获取客户端的 MAC 地址。

      http://www.eggheadcafe.com/community/aspnet/3/10054371/how-to-get-client-mac-address.aspx

      http://codingresource.blogspot.com/2010/02/get-client-mac-address-ip-address-using.html

      【讨论】:

      • 依赖客户端 JavaScript 和第 3 方 Active X 组件是非常不安全和过时的。
      • 是的,这是一件非常可怕的事情。这也是迄今为止给出的唯一正确答案。
      • 我确实怀疑提问者不知道MAC地址和IP地址之间的区别,但事实仍然是可以通过浏览器获取远程客户端的MAC地址。这是一件非常不安全和无用的事情,但这是可能的。
      • 提问者更可能是要获取本地 MAC 地址,而不是客户端。这是一个非常不同的问题,这里的其他答案给出了 PHP 的解决方案。
      【解决方案6】:

      获取客户端设备ip和mac地址

      {
          if (isset($_SERVER['HTTP_CLIENT_IP']))
              $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
          else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
              $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
          else if(isset($_SERVER['HTTP_X_FORWARDED']))
              $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
          else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
              $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
          else if(isset($_SERVER['HTTP_FORWARDED']))
              $ipaddress = $_SERVER['HTTP_FORWARDED'];
          else if(isset($_SERVER['REMOTE_ADDR']))
              $ipaddress = $_SERVER['REMOTE_ADDR'];
          else
              $ipaddress = 'UNKNOWN';
      
          $macCommandString   =   "arp " . $ipaddress . " | awk 'BEGIN{ i=1; } { i++; if(i==3) print $3 }'";
      
          $mac = exec($macCommandString);
      
          return ['ip' => $ipaddress, 'mac' => $mac];
      }
      

      【讨论】:

        【解决方案7】:

        想法是,使用命令cmd ipconfig /all,只提取地址mac。

        他的索引是 $pmac+33。

        而mac的大小是17。

        <?php
        ob_start();
        system('ipconfig /all');
        $mycom=ob_get_contents();
        ob_clean();
        $findme = 'physique';
        $pmac = strpos($mycom, $findme);
        $mac=substr($mycom,($pmac+33),17);
        echo $mac;
        ?>
        

        【讨论】:

          【解决方案8】:
          <?php
          
              ob_start();
              system('ipconfig/all');
              $mycom=ob_get_contents(); 
              ob_clean(); 
              $findme = "Physical";
              $pmac = strpos($mycom, $findme); 
              $mac=substr($mycom,($pmac+36),17);
          
              echo $mac;
          ?>
          

          这会打印客户端机器的mac地址

          【讨论】:

            【解决方案9】:
            ...
            
            function get_remote_macaddr( $ip ) {
            
            return( strtoupper( exec( "arp -a " . $ip . " | awk '{print $4 }'") ) 
            
            }
            
            // test
            $ipaddress = '192.168.20.252';
            echo 'ip: '.$ipaddress.", ".'mac_addr: '.get_remote_macaddr( $ipaddress );
            

            【讨论】:

              【解决方案10】:

              首先您检查您的用户代理操作系统 Linux 或 windows 或其他。 然后你的操作系统 Windows 然后这段代码使用:

              public function win_os(){ 
                  ob_start();
                  system('ipconfig-a');
                  $mycom=ob_get_contents(); // Capture the output into a variable
                  ob_clean(); // Clean (erase) the output buffer
                  $findme = "Physical";
                  $pmac = strpos($mycom, $findme); // Find the position of Physical text
                  $mac=substr($mycom,($pmac+36),17); // Get Physical Address
              
                  return $mac;
                 }
              

              和你的操作系统 Linux Ubuntu 或 Linux 然后这段代码使用:

              public function unix_os(){
                  ob_start();
                  system('ifconfig -a');
                  $mycom = ob_get_contents(); // Capture the output into a variable
                  ob_clean(); // Clean (erase) the output buffer
                  $findme = "Physical";
                  //Find the position of Physical text 
                  $pmac = strpos($mycom, $findme); 
                  $mac = substr($mycom, ($pmac + 37), 18);
              
                  return $mac;
                  }
              

              此代码可能适用于 OS X。

              【讨论】:

                【解决方案11】:
                //Simple & effective way to get client mac address
                // Turn on output buffering
                ob_start();
                //Get the ipconfig details using system commond
                system('ipconfig /all');
                
                // Capture the output into a variable
                
                    $mycom=ob_get_contents();
                
                // Clean (erase) the output buffer
                
                    ob_clean();
                
                $findme = "Physical";
                //Search the "Physical" | Find the position of Physical text
                $pmac = strpos($mycom, $findme);
                
                // Get Physical Address
                $mac=substr($mycom,($pmac+36),17);
                //Display Mac Address
                echo $mac;
                

                【讨论】:

                • PHP 是服务器端,这会显示服务器的详细信息。
                • 对我来说它没有显示任何东西。我有 linux 主机。没有错误,没有警告,没有输出
                • ipconfig 仅适用于 Windows,这是服务器端。在所有现实世界的场景中,在 PHP 中获取客户端 MAC 地址是不可能的,也不应该有任何您想要的情况。
                • 当然有用例。我有一个来自我的 ISP 的静态 IP 地址,我使用它在服务器上执行特殊(通常是调试)代码,仅当我是连接用户时,基于我的 IP 地址。没有其他用户会看到该输出。但是,当我的 ISP 将我切换到光纤时,他们不会提供固定或静态 IP 地址,我需要一种方法来做同样的事情,即使我的 IP 地址每次访问都不相同。我的 MAC 地址不会改变,所以如果我的代码可以确定用户的 MAC 地址并且它是我的,那么它可以安全地执行该特殊代码。
                猜你喜欢
                • 2021-03-18
                • 2017-10-28
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-05-03
                • 2019-07-12
                • 2013-07-07
                相关资源
                最近更新 更多