discuz! X2修改的方法是:用Notepad+打开/source/class/class_core.php 文件,搜索“HTTP_CLIENT_IP”,定位我们要修改的代码。如下图:
3、这上图中三行代码全部删除,记住只删除这三行代码,然后加入以下代码作替代。
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all(‘#/d{1,3}/./d{1,3}/./d{1,3}/./d{1,3}#s’, $_SERVER['HTTP_X_FORWARDED_FOR'],
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all(‘#/d{1,3}/./d{1,3}/./d{1,3}/./d{1,3}#s’, $_SERVER['HTTP_X_FORWARDED_FOR'],
4、Discuz! X2.5修改的方法是:打开/source/class/discuz/discuz_application.php文件,搜索“HTTP_CLIENT_IP”,同样找到这三行,用以上代码替换。如下图:
5、最后的效果如下图:
6、当然不要忘记到Discuz! 后台更新一下缓存。
1、对于正在使用CDN服务的用户,找到这个文件:/source/class/discuz/discuz_application.php,搜索,在378行左右。
private function _get_client_ip() {
$ip = $_SERVER['REMOTE_ADDR'];
if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}/.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#/d{1,3}/./d{1,3}/./d{1,3}/./d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
foreach ($matches[0] AS $xip) {
if (!preg_match('#^(10|172/.16|192/.168)/.#', $xip)) {
$ip = $xip;
break;
}
}
}
return $ip;
}
2、将这一段替换为以下代码:
private function _get_client_ip() {
$ip = $_SERVER['REMOTE_ADDR'];
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#/d{1,3}/./d{1,3}/./d{1,3}/./d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
foreach ($matches[0] AS $xip) {
if (!preg_match('#^(10|172/.16|192/.168)/.#', $xip)) {
$ip = $xip;
break;
}
}
} elseif(isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}/.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
return $ip;
}
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/249250.html