Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function to get user IP address in Yii

Tags:

yii

I'm trying to create a s shortcut to get user IP address, I created this function below in protected/helpers/shortcut.php

echo getIP();
function getIP()
{
    return CHttpRequest::getUserHostAddress();
}

i get this error because i set my php.ini to strict. and getUserHostAddress() is not a static function

Strict Standards: Non-static method CHttpRequest::getUserHostAddress() should not be called statically in /Applications/XAMPP/xamppfiles/htdocs/dev/protected/helpers/shortcuts.php on line 97
::1

i tried

Yii::app()->request->userHostAddress;

but i get this error

Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/dev/protected/helpers/shortcuts.php on line 97

any idea what i'm doing wrong? Thanks

like image 992
user2636556 Avatar asked Mar 11 '14 12:03

user2636556


2 Answers

try this:

Yii::app()->request->getUserHostAddress()

instead

Yii::app()->request->getUserHostAddress

with "()" it should work

like image 180
user3410311 Avatar answered Sep 19 '22 12:09

user3410311


In Yii2, use Yii::$app->getRequest()->getUserIP()

like image 28
S B Avatar answered Sep 21 '22 12:09

S B