How can I get the MAC and the IP address of a connected client in PHP?

前端 未结 16 2352
长发绾君心
长发绾君心 2020-11-22 02:29

I need to know the MAC and the IP address of the connect clients, how can I do this in PHP?

相关标签:
16条回答
  • 2020-11-22 03:07

    You can do this easily using openWRT. If yo use a captive portal you can mix php and openWRT and make a relation between the IP and the mac.

    You can write a simple PHP code using:

     $localIP = getHostByName(getHostName()); 
    

    Later, using openWRT you can go to /tmp/dhcp.leases, you will get something with the form:

     e4:a7:a0:29:xx:xx 10.239.3.XXX DESKTOP-XXX 
    

    There, you have the mac, the IP address and the hostname.

    0 讨论(0)
  • 2020-11-22 03:09

    The MAC address of a client (in the sense of the computer that issued the HTTP request) is overwritten by every router between the client and the server.

    Client IP is conveniently provided to the script in $_SERVER['REMOTE_ADDR']. In some scenarios, particularly if your web server is behind a proxy (i.e. a caching proxy) $_SERVER['REMOTE ADDR'] will return the IP of the proxy, and there will be an extra value, often $_SERVER['HTTP_X_FORWARDED_FOR'], that contains the IP of the original request client.

    Sometimes, particularly when you're dealing with an anonymizing proxy that you don't control, the proxy won't return the real IP address, and all you can hope for is the IP address of the proxy.

    0 讨论(0)
  • 2020-11-22 03:09

    Getting MAC Address Using PHP: (Tested in Ubuntu 18.04) - 2020 Update

    Here's the Code:

    <?php
        $mac = shell_exec("ip link | awk '{print $2}'");
        preg_match_all('/([a-z0-9]+):\s+((?:[0-9a-f]{2}:){5}[0-9a-f]{2})/i', $mac, $matches);
        $output = array_combine($matches[1], $matches[2]);
        $mac_address_values =  json_encode($output, JSON_PRETTY_PRINT);   
        echo $mac_address_values
    ?>
    

    Output:

    {
        "lo": "00:00:00:00:00:00",
        "enp0s25": "00:21:cc:d4:2a:23",
        "wlp3s0": "84:3a:4b:03:3c:3a",
        "wwp0s20u4": "7a:e3:2a:de:66:09"
    }
    
    0 讨论(0)
  • 2020-11-22 03:12
    // Turn on output buffering  
    ob_start();  
    
    //Get the ipconfig details using system commond  
    system('ipconfig /all');  
    
    // Capture the output into a variable  
    $mycomsys=ob_get_contents();  
    
    // Clean (erase) the output buffer  
    ob_clean();  
    
    $find_mac = "Physical"; 
    //find the "Physical" & Find the position of Physical text  
    
    $pmac = strpos($mycomsys, $find_mac);  
    // Get Physical Address  
    
    $macaddress=substr($mycomsys,($pmac+36),17);  
    //Display Mac Address  
    
    echo $macaddress;  
    

    This works for me on Windows, as ipconfig /all is Windows system command.

    0 讨论(0)
提交回复
热议问题