CloudFlare并通过PHP记录访问者IP地址 [英] CloudFlare and logging visitor IP addresses via in PHP
问题描述
我正在尝试跟踪和记录使用PHP的$_SERVER['REMOTE_ADDR']
访问我的网站的用户/访问者. PHP中IP地址跟踪的一种典型方法.
但是,我正在使用CloudFlare进行缓存等,并接收其CloudFlare的IP地址:
108.162.212.*-108.162.239.*
在仍然使用CloudFlare的同时检索实际用户/访问者IP地址的正确方法是什么?
可用于云弹的其他服务器变量是:
$_SERVER["HTTP_CF_CONNECTING_IP"]
真正的访问者IP地址,这就是您想要的
$_SERVER["HTTP_CF_IPCOUNTRY"]
访问者国家/地区
$_SERVER["HTTP_CF_VISITOR"]
这可以帮助您了解其http还是https
您可以像这样使用它:
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
如果执行此操作,并且访问IP地址的有效性很重要,则可能需要验证$_SERVER["REMOTE_ADDR"]
包含实际有效的cloudflare IP地址,因为如果有人能够直接连接,则任何人都可以伪造标头到服务器IP.
I'm trying to track and log users/visitors that are accessing my website using PHP's $_SERVER['REMOTE_ADDR']
to do so. A typical method for IP address tracking in PHP.
However, I am using CloudFlare for caching and such and receiving their IP addresses as CloudFlare's:
108.162.212.* - 108.162.239.*
What would be a correct method of retrieving the actual users/visitors IP address while still using CloudFlare?
Extra server variables that are available to cloud flare are:
$_SERVER["HTTP_CF_CONNECTING_IP"]
real visitor ip address, this is what you want
$_SERVER["HTTP_CF_IPCOUNTRY"]
country of visitor
$_SERVER["HTTP_CF_VISITOR"]
this can help you know if its http or https
you can use it like this:
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
If you do this, and the validity of the visiting IP address is important, you might need to verify that the $_SERVER["REMOTE_ADDR"]
contains an actual valid cloudflare IP address, because anyone can fake the header if he was able to connect directly to the server IP.
这篇关于CloudFlare并通过PHP记录访问者IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!