Skip to content

Instantly share code, notes, and snippets.

@goocarlos
Created August 17, 2013 08:38
Show Gist options
  • Save goocarlos/6255944 to your computer and use it in GitHub Desktop.
Save goocarlos/6255944 to your computer and use it in GitHub Desktop.
扩展 Input 库解决 Codeigniter 在中国的 IP 地址问题
<?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