简体   繁体   English

Laravel 请求显示 IP 地址的“::1”

[英]Laravel request showing '::1' for IP address

I'm trying to find the user's IP address in Laravel.我正在尝试在 Laravel 中查找用户的 IP 地址。 request->ip() seems like it would do the job, but that just returns ::1 . request->ip()似乎可以完成这项工作,但这只是返回::1 I also tried $_SERVER['REMOTE_ADDR'] , but get the same;我也试过$_SERVER['REMOTE_ADDR'] ,但得到相同的结果; $_SERVER returns the following for me: $_SERVER为我返回以下内容:

array:24 [▼
  "DOCUMENT_ROOT" => "xxx"
  "REMOTE_ADDR" => "::1"
  "REMOTE_PORT" => "51995"
  "SERVER_SOFTWARE" => "PHP 5.5.32 Development Server"
  ...
]

The REMOTE_ADDR is always ::1 for me. REMOTE_ADDR对我来说总是::1 I've tried 3 different WiFi networks.我尝试了 3 种不同的 WiFi 网络。 Am I doing something wrong?我做错了什么吗?

Edit: In the $_SERVER associative array, there isn't any forwarding, so I wouldn't expect localhost.编辑:在$_SERVER关联数组中,没有任何转发,所以我不期望 localhost。

Edit编辑

I understand that this is my local machine.我知道这是我的本地机器。 Is there a way to stub that out in testing so I can make assertions on it (eg, that it matches a certain format)?有没有办法在测试中将其存根,以便我可以对其进行断言(例如,它匹配某种格式)?

I assume you're running your website on the same server / computer you're making the request.我假设您在提出请求的同一台服务器/计算机上运行您的网站。

::1 is 127.0.0.1 or localhost in IPv6 (your current netcard address). ::1127.0.0.1或 IPv6 中的localhost (您当前的网卡地址)。

So if you put your website on a distant server, you'll get the user IP address.因此,如果您将网站放在远程服务器上,您将获得用户 IP 地址。

I guess you can choose the IPv4 or IPv6 display in your config file.我猜您可以在配置文件中选择 IPv4 或 IPv6 显示。

发生这种情况,如果您的项目位于本地主机上,请上传到服务器。

That's probably because $_SERVER['REMODE_ADDR'] is only defined when there's, well, a remote address, ie somebody requests your page from a browser.这可能是因为 $_SERVER['REMODE_ADDR'] 仅在存在远程地址时才定义,即有人从浏览器请求您的页面。 With cron the script is executed locally and the setting may be empty.使用 cron 脚本在本地执行并且设置可能为空。 If it breaks your code, try setting it yourself at the start of the script:如果它破坏了您的代码,请尝试在脚本开始时自行设置:

if( !isset($_SERVER['REMODE_ADDR']) ) {
   $_SERVER['REMODE_ADDR'] = '127.0.0.1';
}

more here Undefined REMOTE_ADDR when using Cron Jobs - Backpack for Laravel 使用 Cron 作业时未定义的 REMOTE_ADDR - Laravel 背包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM