Skip to content

Instantly share code, notes, and snippets.

@cjbell
Created January 30, 2012 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjbell/1705440 to your computer and use it in GitHub Desktop.
Save cjbell/1705440 to your computer and use it in GitHub Desktop.
MY_Input ip_address while under ELB
<?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