this will tell Yii Fw to use the new NTDI_CHttpRequest class instead the core CHttpRequest class when dealing with Http Request.
in protected/components/ make a new file with the same name as the new class name .php
NTDI_CHttpRequest.php file:
<?php
interface HttpRequestable {
public function getUserHostAddress();
}
/**
* NTDI_CHttpRequest offers an improved version of the CHttpRequest::getUserHostAddress()
* The method, is a port from Codeigniter w some small mods.
*
* If your server is behind a reverse proxy, you must whitelist the proxy IP
* addresses the Framework should trust the HTTP_X_FORWARDED_FOR
* header in order to properly identify the visitor's IP address.
*
* @author p0pemar1naru
*/
class NTDI_CHttpRequest extends CHttpRequest implements HttpRequestable {
/**
* Reverse Proxy IPs.
* @var string Comma-delimited, e.g. '10.0.1.200,10.0.1.201'
*/
protected $ntdi_proxyIP = '';
/**
* IP address of the current user
* @var boolean
*/
private $ntdi_IPAddress = FALSE;
public function init()
{
parent::init();
}
/**
* Fetch the IP Address. Determines and validates the visitor's IP address.
* @return string IP address
*/
public function getUserHostAddress()
{
if ($this->ntdi_IPAddress !== FALSE)
return $this->ntdi_IPAddress;
if ($this->ntdi_proxyIP != '' && isset($_SERVER['HTTP_X_FORWARDED_FOR']) && isset($_SERVER['REMOTE_ADDR'])) {
$proxies = preg_split('/[\s,]/', $this->ntdi_proxyIP, -1, PREG_SPLIT_NO_EMPTY);
$proxies = is_array($proxies) ? $proxies : array($proxies);
$this->ntdi_IPAddress = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
} elseif (isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['HTTP_CLIENT_IP']))
$this->ntdi_IPAddress = $_SERVER['HTTP_CLIENT_IP'];
elseif (isset($_SERVER['REMOTE_ADDR']))
$this->ntdi_IPAddress = $_SERVER['REMOTE_ADDR'];
elseif (isset($_SERVER['HTTP_CLIENT_IP']))
$this->ntdi_IPAddress = $_SERVER['HTTP_CLIENT_IP'];
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$this->ntdi_IPAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
if ($this->ntdi_IPAddress === FALSE) {
$this->ntdi_IPAddress = '0.0.0.0';
return $this->ntdi_IPAddress;
}
if (strpos($this->ntdi_IPAddress, ',') !== FALSE) {
$x = explode(',', $this->ntdi_IPAddress);
$this->ntdi_IPAddress = trim(end($x));
}
if (!$this->ntdi_isValidIP($this->ntdi_IPAddress))
$this->ntdi_IPAddress = '0.0.0.0';
return $this->ntdi_IPAddress;
}
/**
* Validate IP Address
* @param string $ip
* @return boolean
*/
public function ntdi_isValidIP($ip)
{
$ip_segments = explode('.', $ip);
// Always 4 segments needed
if (count($ip_segments) != 4)
return FALSE;
// IP can not start with 0
if ($ip_segments[0][0] == '0')
return FALSE;
// Check each segment
foreach ($ip_segments as $segment) {
// IP segments must be digits and can not be
// longer than 3 digits or greater then 255
if ($segment == '' || preg_match("/[^0-9]/", $segment) || $segment > 255 || strlen($segment) > 3)
return FALSE;
}
return TRUE;
}
}
NOTE: the role of the interface is to give me a clear understanding of what method was overriden from core class.