网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的范文:

 

标题 discuz论坛使用cdn后无法获取用户真实ip的解决方法
范文
    该问题存在于任何cdn产品,如果您使用了cdn产品,该discuz方法都适用。
    该问题导致的结果:
    1.discuz论坛可能无法获得用户的真实ip,导致某些用户ip显示的是加速乐节点的ip
    2.论坛访问量过大的话,可能会导致用户访问时提示“抱歉,您的 ip 地址不在被允许,或您的账号被禁用,无法访问本站点”
    产生原因:
    使用cdn,对于网站访客来说,相当于使用了代理访问,而discuz在设计上,是优先获取代理ip,其它才会检测代理服务器是否将用户真实ip传输过来,也就是说获取代理ip优先于用户真实ip。如果您的网站不需要对用户访问做过多的限制,强烈建议按照以下方法进行:
    解决方法(discuz x2):
    打开discuz /source/class/class_core.php 文件
    找到第341行,或者搜索“http_client_ip”,找到如下代码:
    代码如下:
    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;
    }
    将以上代码修改为:
    代码如下:
    function _get_client_ip() {
    $ip = $_server['remote_addr'];
    if (isset($_server['http_x_real_forwarded_for']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_server['http_x_real_forwarded_for'])) {
    $ip = $_server['http_x_real_forwarded_for'];
    }
    elseif (isset($_server['http_x_forwarded_for']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_server['http_x_forwarded_for'])) {
    $ip = $_server['http_x_forwarded_for'];
    }
    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;
    }
    解决方法(discuz x2.5)
    打开文件\source\class\discuz\discuz_application.php 找到如下代码:
    代码如下:
    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;
    }
    将其修改为:
    代码如下:
    private function _get_client_ip() {
    $ip = $_server['remote_addr'];
    if (isset($_server['http_x_real_forwarded_for']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_server['http_x_real_forwarded_for'])) {
    $ip = $_server['http_x_real_forwarded_for'];
    }
    elseif (isset($_server['http_x_forwarded_for']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_server['http_x_forwarded_for'])) {
    $ip = $_server['http_x_forwarded_for'];
    }
    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;
    }
    以上操作后,登陆cdn后台和你的discuz论坛后台分别清除缓存即可。
随便看

 

在线学习网范文大全提供好词好句、学习总结、工作总结、演讲稿等写作素材及范文模板,是学习及工作的有利工具。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/24 15:08:40