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

前端 未结 16 2351
长发绾君心
长发绾君心 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:00

    too late to answer but here is my approach since no one mentioned this here:
    why note a client side solution ?
    a javascript implementation to store the mac in a cookie (you can encrypt it before that)
    then each request must include that cookie name, else it will be rejected.
    to make this even more fun you can make a server side verification
    from the mac address you get the manifacturer (there are plenty of free APIs for this)
    then compare it with the user_agent value to see if there was some sort of manipulation:
    a mac address of HP + a user agent of Safari = reject request.

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

    You can use the following solution to solve your problem:

    $mac='UNKNOWN';
    foreach(explode("\n",str_replace(' ','',trim(`getmac`,"\n"))) as $i)
    if(strpos($i,'Tcpip')>-1){$mac=substr($i,0,17);break;}
    echo $mac;
    
    0 讨论(0)
  • 2020-11-22 03:03

    Server IP

    You can get the server IP address from $_SERVER['SERVER_ADDR'].

    Server MAC address

    For the MAC address, you could parse the output of netstat -ie in Linux, or ipconfig /all in Windows.

    Client IP address

    You can get the client IP from $_SERVER['REMOTE_ADDR']

    Client MAC address

    The client MAC address will not be available to you except in one special circumstance: if the client is on the same ethernet segment as the server.

    So, if you are building some kind of LAN based system and your clients are on the same ethernet segment, then you could get the MAC address by parsing the output of arp -n (linux) or arp -a (windows).

    Edit: you ask in comments how to get the output of an external command - one way is to use backticks, e.g.

    $ipAddress=$_SERVER['REMOTE_ADDR'];
    $macAddr=false;
    
    #run the external command, break output into lines
    $arp=`arp -a $ipAddress`;
    $lines=explode("\n", $arp);
    
    #look for the output line describing our IP address
    foreach($lines as $line)
    {
       $cols=preg_split('/\s+/', trim($line));
       if ($cols[0]==$ipAddress)
       {
           $macAddr=$cols[1];
       }
    }
    

    But what if the client isn't on a LAN?

    Well, you're out of luck unless you can have the client volunteer that information and transmit via other means.

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

    You can get MAC Address or Physical Address using this code

    $d = explode('Physical Address. . . . . . . . .',shell_exec ("ipconfig/all"));  
    $d1 = explode(':',$d[1]);  
    $d2 = explode(' ',$d1[1]);  
    return $d2[1];
    

    I used explode many time because shell_exec ("ipconfig/all") return complete detail of all network. so you have to split one by one. when you run this code then you will get
    your MAC Address 00-##-##-CV-12 //this is fake address for show only.

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

    We can get MAC address in Ubuntu by this ways in php

    $ipconfig =   shell_exec ("ifconfig -a | grep -Po 'HWaddr \K.*$'");  
        // display mac address   
     echo $ipconfig;
    
    0 讨论(0)
  • 2020-11-22 03:07

    All you need to do is to put arp into diferrent group.

    Default:

    -rwxr-xr-x 1 root root 48K 2008-11-11 18:11 /usr/sbin/arp*
    

    With command:

    sudo chown root:www-data /usr/sbin/arp
    

    you will get:

    -rwxr-xr-x 1 root www-data 48K 2008-11-11 18:11 /usr/sbin/arp*
    

    And because apache is a daemon running under the user www-data, it's now able to execute this command.

    So if you now use a PHP script, e.g.:

    <?php
    $mac = system('arp -an');
    echo $mac;
    ?>
    

    you will get the output of linux arp -an command.

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