Created
August 17, 2013 08:38
-
-
Save goocarlos/6255944 to your computer and use it in GitHub Desktop.
扩展 Input 库解决 Codeigniter 在中国的 IP 地址问题
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); | |
/** | |
* 扩展的 Input 库 | |
* @author Luyu<luyu.zhang@autotiming.com> | |
*/ | |
class MY_Input extends CI_Input | |
{ | |
function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* 解决 CI 获取 IP 地址的问题 | |
* @author Luyu<luyu.zhang@autotiming.com> | |
* @return string | |
*/ | |
public function ip_address() | |
{ | |
if ($this->ip_address !== FALSE) { | |
return $this->ip_address; | |
} | |
// TODO: $proxy_ips | |
if (isset($_SERVER['HTTP_CDN_SRC_IP']) && !empty($_SERVER['HTTP_CDN_SRC_IP'])) { | |
$correct_ip_address = $_SERVER['HTTP_CDN_SRC_IP']; | |
} else if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && !empty($_SERVER["HTTP_X_FORWARDED_FOR"])) | |
$correct_ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"]; | |
else if (isset($_SERVER["HTTP_CLIENT_IP"]) && !empty($_SERVER["HTTP_CLIENT_IP"])) | |
$correct_ip_address = $_SERVER["HTTP_CLIENT_IP"]; | |
else if (isset($_SERVER["REMOTE_ADDR"]) && !empty($_SERVER["REMOTE_ADDR"])) | |
$correct_ip_address = $_SERVER["REMOTE_ADDR"]; | |
else | |
$correct_ip_address = '0.0.0.0'; | |
$this->ip_address = $correct_ip_address; | |
if (!$this->valid_ip($this->ip_address)) { | |
$this->ip_address = '0.0.0.0'; | |
} | |
return $this->ip_address; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment