5

I am working on laravel project. I need to access client's IP Address and MAC Address those who access the website.

Is their any way to get both the Addresses.

I've used:

Request::ip();

I got the IP address of client. But how to get MAC address.

Thanks in advance.

Reema Parakh
  • 1,347
  • 3
  • 19
  • 46
Bhuvanesh Soni
  • 206
  • 1
  • 2
  • 9
  • 2
    [Here](https://stackoverflow.com/questions/3309122/how-can-i-get-a-mac-address-from-an-http-request) is a good answer to the question. – nakov Feb 19 '19 at 04:22
  • 2
    Possible duplicate of [How can I get the MAC and the IP address of a connected client in PHP?](https://stackoverflow.com/questions/1420381/how-can-i-get-the-mac-and-the-ip-address-of-a-connected-client-in-php) – Mostafa Hussein Feb 19 '19 at 05:55

3 Answers3

3

Yes You Can Do it

BY DEFAULT PHP HAS A BUILT IN FUNCTION THAT EXECUTES THE COMMAND LINE COMMANDS

shell_exec

http://php.net/manual/en/function.shell-exec.php

exec

http://php.net/manual/en/function.exec.php

So To get the mac address have written the function

 function getMAcAddressExec()
{
        return substr(exec('getmac'), 0, 17); 
}
echo getMAcAddressExec();


function getMAcAddressShellExec()
{
        return substr(shell_exec('getmac'), 159,20); 
}
echo getMAcAddressShellExec();

EDITED

add follwowing lines in web.php file in routes folder

Route::get('/getmacshellexec',function()
    {
        $shellexec = shell_exec('getmac'); 
        dd($shellexec);
    }
);

Route::get('/getmacexec',function()
    {
        $shellexec = exec('getmac'); 
        dd($shellexec);
    }
);

And Try the url

yourproject/getmacshellexec  

AND

yourproject/getmacexec

And Kindly Comment Below of you get any any output

ManojKiran A
  • 5,896
  • 4
  • 30
  • 43
0

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). 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.

Hamza Dastgir
  • 155
  • 1
  • 5
0

The exec() is a function that is used to run an external program in PHP. It returns the last line from the result of the command. To get the MAC address, pass the parameter getmac which returns the MAC address of the client. getmacis a CMD command to get the MAC address.

To get the MAC address, we use exec() function.

$macAddr = exec('getmac');

For getting the IP address we have to include use Illuminate\Http\Request; in the Controller and then add the code of the below pre tag. It will give the AP address of the network.

$ipAddr=\Request::ip();