laravel 如何在laravel中获取客户端IP地址和MAC地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54758803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Client IP Address and MAC Address in laravel?
提问by Bhuvanesh Soni
I am working on laravel project. I need to access client's IP Address and MAC Address those who access the website.
我正在从事 Laravel 项目。我需要访问访问该网站的人的客户端 IP 地址和 MAC 地址。
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.
我得到了客户端的IP地址。但是如何获得MAC地址。
Thanks in advance.
提前致谢。
回答by Manojkiran.A
Yes You Can Do it
是的,你可以做到
BY DEFAULT PHP HAS A BUILT IN FUNCTION THAT EXECUTES THE COMMAND LINE COMMANDS
默认情况下,PHP 内置了一个执行命令行命令的函数
shell_exec
shell_exec
http://php.net/manual/en/function.shell-exec.php
http://php.net/manual/en/function.shell-exec.php
exec
执行
http://php.net/manual/en/function.exec.php
http://php.net/manual/en/function.exec.php
So To get the mac address have written the function
所以为了得到mac地址已经写了函数
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
在web.php
文件routes
夹中的文件中添加以下行
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
并请在下面评论你得到任何输出
回答by Hamza Dastgir
Server IPYou can get the server IP address from $_SERVER['SERVER_ADDR'].
服务器IP您可以从$_SERVER['SERVER_ADDR'] 获取服务器IP 地址。
Server MAC addressFor the MAC address, you could parse the output of netstat -ie in Linux, or ipconfig /all in Windows.
服务器MAC地址对于MAC地址,Linux下解析netstat -ie,Windows下解析ipconfig /all。
Client IP addressYou can get the client IP from $_SERVER['REMOTE_ADDR']
客户端IP地址可以从$_SERVER['REMOTE_ADDR']获取客户端IP
Client MAC addressThe 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.
客户端 MAC 地址 除非在一种特殊情况下,否则您将无法使用客户端 MAC 地址:如果客户端与服务器位于同一以太网段。
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?
因此,如果您正在构建某种基于 LAN 的系统并且您的客户端位于同一以太网段上,那么您可以通过解析 arp -n (linux) 或 arp -a (windows) 的输出来获取 MAC 地址。 但是如果客户端不在 LAN 上呢?
Well, you're out of luck unless you can have the client volunteer that information and transmit via other means.
好吧,除非您可以让客户自愿提供该信息并通过其他方式传输,否则您就不走运了。