$_SERVER['REMOTE_ADDR'] 没有给出正确的 IP 地址

2022-01-25 00:00:00 ip localhost php

我正在用 PHP 制作一个表格,我想记录用户的 IP 地址.这是我使用的代码片段:

I'm making a form with PHP and I want to keep record of the User's IP Addresses. This is the snip-it of code I used:

<input type="hidden" name="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" />

当我在 XAMPP 中打开代码并读取源代码时,该值的 IP 地址与我的不同:

When I open the code up in XAMPP and read the source, the value had an IP address different than what was mine:

<input type="hidden" name="ip" value="::1" />

当我在 localhost (XAMPP) 中使用此 IP 地址时,通常会出现此 IP 地址吗?
如果没有,是否有其他方法可以获取用户的 IP 地址?

Does this IP address normally happen when I use it in a localhost (XAMPP)?
If not, are there any alternatives into grabbing the user's IP address?

推荐答案

<input type="hidden" name="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>"/>

不要那样做.提交表单时从 $_SERVER 获取请求.在生成表单时获取它并将其存储在表单中只是让人们有机会更改它.

Don't do that. Get the request from $_SERVER when the form is submitted. Getting it when the form is generated and storing it in the form just gives people the opportunity to change it.

当我在 localhost (XAMPP) 中使用此 IP 地址时,通常会出现此 IP 地址吗?

是的.从 localhost 请求页面时,获取本地 IP(IPv6)地址是正常的.

Yes. Getting the local IP (IPv6) address is normal when you request a page from localhost.

相关文章