我正在尝试创建一个获取用户IP地址的快捷方式,我在 protected / helpers / shortcut.php
中创建了此功能。echo getIP();
function getIP()
{
return CHttpRequest::getUserHostAddress();
}
我收到此错误,因为我将php.ini设置为严格。和getUserHostAddress()不是静态函数
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
我试过
Yii::app()->request->userHostAddress;
但我收到此错误
Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/dev/protected/helpers/shortcuts.php on line 97
知道我做错了什么吗?感谢
答案 0 :(得分:45)
在Yii2中,使用Yii::$app->getRequest()->getUserIP()
答案 1 :(得分:16)
试试这个:
Yii::app()->request->getUserHostAddress()
而不是
Yii::app()->request->getUserHostAddress
用“()”它应该工作