我正在尝试创建一个 s 快捷方式来获取用户 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
知道我在做什么错吗?谢谢
最佳答案
在 Yii2 中,使用 Yii::$app->getRequest()->getUserIP()
关于yii - Yii中获取用户IP地址的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22325979/