該問題存在于任何cdn產(chǎn)品,如果您使用了cdn產(chǎn)品,該discuz方法都適用。
該問題導(dǎo)致的結(jié)果:
1.discuz論壇可能無法獲得用戶的真實(shí)ip,導(dǎo)致某些用戶ip顯示的是加速樂節(jié)點(diǎn)的ip
2.論壇訪問量過大的話,可能會導(dǎo)致用戶訪問時提示“抱歉,您的 ip 地址不在被允許,或您的賬號被禁用,無法訪問本站點(diǎn)”
產(chǎn)生原因:
使用cdn,對于網(wǎng)站訪客來說,相當(dāng)于使用了代理訪問,而discuz在設(shè)計上,是優(yōu)先獲取代理ip,其它才會檢測代理服務(wù)器是否將用戶真實(shí)ip傳輸過來,也就是說獲取代理ip優(yōu)先于用戶真實(shí)ip。如果您的網(wǎng)站不需要對用戶訪問做過多的限制,強(qiáng)烈建議按照以下方法進(jìn)行:
解決方法(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論壇后臺分別清除緩存即可。