Created
January 30, 2012 17:03
-
-
Save cjbell/1705440 to your computer and use it in GitHub Desktop.
MY_Input ip_address while under ELB
This file contains hidden or 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 | |
class MY_Input extends CI_Input { | |
function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Fetch the IP Address | |
* | |
* @access public | |
* @return string | |
* | |
* UPDATED TO RETRIEVE UNDERNEATH AMAZON ELB | |
*/ | |
function ip_address() | |
{ | |
if ($this->ip_address !== FALSE) | |
{ | |
return $this->ip_address; | |
} | |
if (config_item('proxy_ips') != '' && $this->server('HTTP_X_FORWARDED_FOR') && $this->server('REMOTE_ADDR')) | |
{ | |
$proxies = preg_split('/[\s,]/', config_item('proxy_ips'), -1, PREG_SPLIT_NO_EMPTY); | |
$proxies = is_array($proxies) ? $proxies : array($proxies); | |
$this->ip_address = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; | |
} | |
elseif ($this->server('REMOTE_ADDR') AND $this->server('HTTP_CLIENT_IP')) | |
{ | |
$this->ip_address = $_SERVER['HTTP_CLIENT_IP']; | |
} | |
elseif ($this->server('REMOTE_ADDR')) | |
{ | |
$this->ip_address = ($this->server('CLIENTIP')) ? $_SERVER['CLIENTIP'] : $_SERVER['REMOTE_ADDR']; | |
// Ammended from: | |
// $this->ip_address = $_SERVER['REMOTE_ADDR']; | |
// Eg. use the CLIENTIP item that Amazon sets | |
} | |
elseif ($this->server('HTTP_CLIENT_IP')) | |
{ | |
$this->ip_address = $_SERVER['HTTP_CLIENT_IP']; | |
} | |
elseif ($this->server('HTTP_X_FORWARDED_FOR')) | |
{ | |
$this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
} | |
if ($this->ip_address === FALSE) | |
{ | |
$this->ip_address = '0.0.0.0'; | |
return $this->ip_address; | |
} | |
if (strpos($this->ip_address, ',') !== FALSE) | |
{ | |
$x = explode(',', $this->ip_address); | |
$this->ip_address = trim(end($x)); | |
} | |
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