[英]Getting Client's IP in laravel
I am new to website development.我是网站开发的新手。
I need to capture the client's IP address.我需要捕获客户端的 IP 地址。 I checked similar question
https://stackoverflow.com/questions/33268683/how-to-get-client-ip-address-in-laravel-5
but couldn't understand much.我检查了类似的问题
https://stackoverflow.com/questions/33268683/how-to-get-client-ip-address-in-laravel-5
但不太明白。
I used request()->ip()
but it gave me google cloud's ip everytime as website is on it and 127.0.0.1 locally.我使用了
request()->ip()
但它每次都给了我谷歌云的 ip,因为网站在上面,在本地是 127.0.0.1。 I want to get client's ip() who visits my website.我想获得访问我网站的客户的 ip()。 How to get it?
如何获得?
I used $myPublicIP = trim(shell_exec("dig +short myip.opendns.com @resolver1.opendns.com"));
我用
$myPublicIP = trim(shell_exec("dig +short myip.opendns.com @resolver1.opendns.com"));
which locally give me my public IP address.在本地给我我的公共 IP 地址。 Will this work when I upload my code on google cloud?
当我将我的代码上传到谷歌云时,这会起作用吗?
and also can someone tell me $myPublicIP = trim(shell_exec("dig +short myip.opendns.com @resolver1.opendns.com"));
还有人可以告诉我
$myPublicIP = trim(shell_exec("dig +short myip.opendns.com @resolver1.opendns.com"));
how this command works?这个命令是如何工作的?
This line:这一行:
$myPublicIP = trim(shell_exec("dig +short myip.opendns.com @resolver1.opendns.com"));
is not checking for the visitor IP. You are checking the IP from the command runner (aka you / your server).不检查访客 IP。您正在从命令运行器(又名您/您的服务器)检查 IP。
Now, $request->ip()
just reads the REMOTE_ADDR
header attribute.现在,
$request->ip()
只读取REMOTE_ADDR
header 属性。
I haven't used Google Cloud and I'm not sure about its behaviour, but if you're getting the Google Cloud's IP instead of the user's IP it must be because it is working as a proxy and it's not forwarding the real user IP.我没有使用过谷歌云,我不确定它的行为,但如果你得到的是谷歌云的 IP 而不是用户的 IP,那一定是因为它作为代理工作,它没有转发真实用户 IP .
I guess there should be a way to tell Google Cloud to forward the visitor IP on the request.我想应该有一种方法可以告诉谷歌云根据请求转发访问者 IP。
If you're using Google Cloud's load balancer, I believe you can access the forwarded IP address from $_SERVER['X-Forwarded-For']
.如果您使用的是 Google Cloud 的负载均衡器,我相信您可以从
$_SERVER['X-Forwarded-For']
访问转发的 IP 地址。
https://cloud.google.com/load-balancing/docs/https#x-forwarded-for_header https://cloud.google.com/load-balancing/docs/https#x-forwarded-for_header
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.