$_SERVER['REMOTE_ADDR'] 没有给出正确的 IP 地址 [英] $_SERVER['REMOTE_ADDR'] not giving the right ip address

查看:141
本文介绍了$_SERVER['REMOTE_ADDR'] 没有给出正确的 IP 地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 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.

这篇关于$_SERVER['REMOTE_ADDR'] 没有给出正确的 IP 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆