以下为常见的PHP网站使用免备案CDN后,如何获取访客真实IP地址的方法,仅供参考.
Discuz!使用CDN后获取真实IP方法
include/common.inc.php
找到如下代码:
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$onlineip = $_SERVER['REMOTE_ADDR'];
}
替换为:
function get_real_ip()
{
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"]))
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip)
{
array_unshift($ips, $ip); $ip = FALSE;
}
for ($i = 0; $i < count($ips); $i++)
{
if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i]))
{
$ip = $ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
$onlineip = get_real_ip();
===========================================================
Supesite使用CDN后获取真实IP 办法
include/main.inc.php文件,这部分
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$_SGLOBAL['onlineip'] = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$_SGLOBAL['onlineip'] = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$_SGLOBAL['onlineip'] = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$_SGLOBAL['onlineip'] = $_SERVER['REMOTE_ADDR'];
}
替换为
function get_real_ip()
{
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"]))
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip)
{
array_unshift($ips, $ip); $ip = FALSE;
}
for ($i = 0; $i < count($ips); $i++)
{
if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i]))
{
$ip = $ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
$_SGLOBAL['onlineip'] = get_real_ip();
======================================================================
帝国cms后台记录的ip日志和用户注册记录的ip
\e\class\connect.php搜索function egetip()函数中的获取ip代码修改成以上部分
DZ注册用户IP和邀请码拥有者IP相同的解决办法
方法A:
register.php 200行
showmessage('register_invite_iperror');
改为
//showmessage('register_invite_iperror');
方法B:
在include/common.inc.php文件中把
代码:
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$onlineip = $_SERVER['REMOTE_ADDR'];
}
替换为:
代码:
$onlineip=getenv('HTTP_X_FORWARD_FOR');
因尔特网络数据中心
2013-11-1