我开始对已通过身份验证的用户和未通过身份验证的用户进行一些区分。为此,我正在使用
Yii::app()->user->id;
但是,在确定的视图中,我输入了以下代码:
<?php
if(Yii::app()->user->isGuest) {
print("Welcome back Guest!");
print("Your id is ".Yii::app()->user->id);
} else {
print("Welcome back ".Yii::app()->user->name);
print("Your id is ".Yii::app()->user->id);
}?>
无论我是否登录(成功),我总是收到“欢迎回来的客人!”。如果我已经登录,那么它会显示欢迎消息以及用户的ID!
EDIT
@briiC.lv
嘿..抱歉回复晚了,我希望你仍然关注这个!我不会扩展给定的 UserIdentity 类。这是强制性的吗?由于我仍然不太清楚整个授权问题,我认为最好尝试一下他们提供的类,然后使用我自己的功能进行扩展。无论如何,接下来我将发布我的 UserIdentity 类及其小的调整。 .也许问题就出在这里?
<?php class UserIdentity extends CUserIdentity{
private $_id;
public function authenticate()
{
$user = Users::model()->findAll('username=\''.$this->username.'\' AND password=\''.$this->encryptedPassword.'\'');
if(!isset($user[0]))
{
return false;
}
else
{
$this->setState('id', $user[0]->id);
$this->username = $user[0]->username;
$this->errorCode=self::ERROR_NONE;
return true;
}
}
public function getId()
{
return $this->_id;
}
}
这是我按照您的建议开始记录时得到的输出;成功登录后我立即得到此输出。
[05:23:21.833][trace][vardump] CWebUser#1 (
[allowAutoLogin] => true
[guestName] => 'Guest'
[loginUrl] => array ( '0' => '/site/login' )
[identityCookie] => null
[authTimeout] => null
[autoRenewCookie] => false
[autoUpdateFlash] => true
[CWebUser:_keyPrefix] => '0f4431ceed8f17883650835e575b504b'
[CWebUser:_access] => array()
[behaviors] => array()
[CApplicationComponent:_initialized] => true
[CComponent:_e] => null
[CComponent:_m] => null
)
任何帮助深表感谢!
也许你可以尝试更努力地调试:
将消息更改为如下内容:
if(Yii::app()->user->isGuest) {
print("Not logged");
} else {
print_r(Yii::app()->user);
print("Welcome ".Yii::app()->user->name);
print("Your id is ".Yii::app()->user->id);
}
并检查 config/main.php 文件中的会话变量
...
'session' => array(
'autoStart'=>true,
),
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)