php:获取IP地址 [英] php: getting ip address

查看:66
本文介绍了php:获取IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得访客的IP地址. 你能告诉我应该使用$_SERVER[]的哪个元素吗?

I want to get an ip address of visitors. could you tell me what element of $_SERVER[] i should use?

$_SERVER['HTTP_CLIENT_IP'];
$_SERVER['HTTP_X_FORWARDED_FOR'];
or
$_SERVER['REMOTE_ADDR'];

更新:

如果您的客户端通过代理服务器连接到Internet,则PHP中的$ _SERVER ['REMOTE_ADDR']只会返回代理服务器的IP地址,而不是客户端计算机的IP地址.确定使用PHP的客户端计算机的确切IP地址,它们是HTTP_CLIENT_IP和HTTP_X_FORWARDED_FOR.

If your client is connected to the Internet through Proxy Server then $_SERVER['REMOTE_ADDR'] in PHP just returns the the IP address of the proxy server not of the client’s machine.There are extra Server variable which might be available to determine the exact IP address of the client’s machine in PHP, they are HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR.

推荐答案

$_SERVER['REMOTE_ADDR'];

根据 PHP文档: 用户正在从中查看当前页面的IP地址.

According to the PHP documentation: The IP address from which the user is viewing the current page.

这是连接到您服务器的IP(由服务器报告).
其他值由客户端设置.

This is the IP that is connected to your server (reported by your server).
The other values are set by the client.

HTTP_X_FORWARDED_FOR是非标准标头(因此带有x前缀),由某些代理服务器设置.大型代理服务器供应商试图帮助ISP识别滥用的IP地址.它包含具有所有转发IP的列表.

The HTTP_X_FORWARDED_FOR is a non-standard header (hence the x-prefix), set by certain proxy-servers. It is an attempt by the big proxy server vendors to help ISPs identify abusive IP addresses; it contains a list with all forwarded-for IPs.

我不知道HTTP_CLIENT_IP标头的来源

这篇关于php:获取IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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