Added method to get real IP address

pull/20/head
Mike Cao 12 years ago
parent 9a115b84ab
commit 37c50c5eee

@ -46,7 +46,7 @@ class Request {
'base' => str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])),
'method' => $_SERVER['REQUEST_METHOD'],
'referrer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
'ip' => $_SERVER['REMOTE_ADDR'],
'ip' => $this->getIpAddress(),
'ajax' => isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') : false,
'scheme' => $_SERVER['SERVER_PROTOCOL'],
'user_agent' => $_SERVER['HTTP_USER_AGENT'],
@ -103,5 +103,32 @@ class Request {
return $params;
}
/**
* Gets the real remote IP address.
*
* @return string IP address
*/
private function getIpAddress() {
static $vars = array(
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'REMOTE_ADDR'
);
foreach ($vars as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
return $ip;
}
}
}
}
}
}
?>

Loading…
Cancel
Save