help me. I don’t know how to get IP address
help me. I don’t know how to get IP address
http://www.yiiframework.com/doc/api/CHttpRequest#getUserHostAddress-detail
<?php
echo 'Your IP adress is: ' . CHttpRequest::getUserHostAddress();
?>
Does this return the REAL user ip even if user is behind firewall?
To get REAL ip address i’m using this function…
public function getRealIp()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
There’s no way to find out the real IP for most users behind a firewall. Simple IP masquerading for example doesn’t add this header information. I also wonder what that IP should be useful for: They mostly use adress ranges from private networks (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16).
I believe that this method is not static, so you may want to use Yii::app()->request->userHostAddress instead.