php 获取cdn 客户端,常见PHP网站使用CDN后如何获取真实IP地址

以下为常见的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

93dea32fc6f78387ab55be722e608050.png

原文链接:https://blog.csdn.net/weixin_39538687/article/details/115153348?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165918471416782425115318%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=165918471416782425115318&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-21-115153348-null-null.nonecase&utm_term=%E5%85%8D%E5%A4%87%E6%A1%88cdn

原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/3793

(0)
上一篇 2022年7月30日 21:54
下一篇 2022年7月30日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

优速盾注册领取大礼包www.cdnb.net
/sitemap.xml