OldCat's Diary

oldcat

Typecho启用cdn后获取真实IP

December 23rd, 2015 3:23 AMNo CommentsFiled under: 网站技术Feed for this Entry

早上发现评论的IP不对

修改var\Typecho\Request.php653行左右

/**
* 设置ip地址
*
* @access public
* @param string $ip
*/
public function setIp($ip = NULL)
{
if (!empty($ip)) {
$this->_ip = $ip;
} else {
switch (true) {
case defined('__TYPECHO_IP_SOURCE__') && NULL !== $this->getServer(__TYPECHO_IP_SOURCE__):
list($this->_ip) = array_map('trim', explode(',', $this->getServer(__TYPECHO_IP_SOURCE__)));
break;
case NULL !== $this->getServer('REMOTE_ADDR'):
$this->_ip = $this->getServer('REMOTE_ADDR');
break;
case NULL !== $this->getServer('HTTP_CLIENT_IP'):
$this->_ip = $this->getServer('HTTP_CLIENT_IP');
break;
default:
break;
}
}

if (empty($this->_ip) || !self::_checkIp($this->_ip)) {
$this->_ip = 'unknown';
}
}
添加一个条件

/**
* 设置ip地址
*
* @access public
* @param string $ip
*/
public function setIp($ip = NULL)
{
if (!empty($ip)) {
$this->_ip = $ip;
} else {
switch (true) {
case defined('__TYPECHO_IP_SOURCE__') && NULL !== $this->getServer(__TYPECHO_IP_SOURCE__):
list($this->_ip) = array_map('trim', explode(',', $this->getServer(__TYPECHO_IP_SOURCE__)));
break;
case NULL !== $this->getServer('HTTP_X_FORWARDED_FOR'):
$this->_ip = $this->getServer('HTTP_X_FORWARDED_FOR');
break;
case NULL !== $this->getServer('REMOTE_ADDR'):
$this->_ip = $this->getServer('REMOTE_ADDR');
break;
case NULL !== $this->getServer('HTTP_CLIENT_IP'):
$this->_ip = $this->getServer('HTTP_CLIENT_IP');
break;
default:
break;
}
}

if (empty($this->_ip) || !self::_checkIp($this->_ip)) {
$this->_ip = 'unknown';
}
}

转自:http://www.xiaosb.cn/135.html

TAGS: Null

Leave a Comment: