简体   繁体   English

在 laravel 中获取客户的 IP

[英]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.

相关问题 将 laravel 从 5.5 更新到 5.7 时出错(未定义 class 常量“HEADER_CLIENT_IP”) - Getting error while updating laravel from 5.5 to 5.7 (Undefined class constant 'HEADER_CLIENT_IP') 如何从 Laravel 的 config/app.php 中获取客户端 ip 地址? - How to get the client ip address from Laravel's config/app.php? 如何在 Laravel 5+ 中获取客户端 IP 地址 - How to get client IP address in Laravel 5+ 通过Docker容器和Z62E0B5B350C9E44D2C19AA801D在Laravel中获取用户的IP地址 - Getting a user's IP address in Laravel through Docker containers and Nginx webserver 用户IP未被记录 - User ip's are not getting logged Laravel 请求返回负载均衡器 ip 而不是客户端 ip - Laravel Request returning load balancer ip instead of client ip 客户端IP地址受到XSS攻击 - XSS attack in client's IP address Laravel:每个用户一个 IP,以及如何从 Laravel 服务器为客户端 IP 提供会话 - Laravel: One IP per User, and how to give session to client IP from Laravel server 如何在laravel中获取客户端IP地址和MAC地址? - How to get Client IP Address and MAC Address in laravel? 如果客户端将使用 Web 服务器的虚拟 IP 浏览,是否可以使用 PHP 获取客户端的真实 IP 地址? - Is it possible to get the client's REAL IP address using PHP IF the client will browse using the Virtual IP of the Webserver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM