我刚刚用yii登录。在我的登录表中有一个"usertype"列,所以为了检查它是否正常工作,我在代码中放入了一个文件编写器,但每次代码运行时都不会被写入(忘记用户类型,甚至不是字符串)。以下是代码
组件/用户标识
<?php
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$user = Trackaccountusers::model()->findByAttributes(array('username'=>$this->username));
if ($user===null) { // No user found!
$this->errorCode=self::ERROR_USERNAME_INVALID;
} else if ($user->password !== ($this->password) ) { // Invalid password!
$this->errorCode=self::ERROR_PASSWORD_INVALID;
} else { // Okay!
$this->errorCode=self::ERROR_NONE;
// Store the role in a session:
$this->setState('usertype', $user->usertype);
$file = "log.txt";
$write = ("time_s ".$user->usertype."'n");//
file_put_contents($file, $write, FILE_APPEND);
//$this->_id = $user->id;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
出现这种问题有两个原因。
- 检查您是否具有对组件目录的写入权限。您可以使用
chmod -R +x components
进行设置 -
检查组件目录中是否有apache/nnix用户所有者。您可以使用
chown -R www:data:www:data components
进行设置 -
在您的视图/控制器中使用以下代码来检查是否设置了Yii状态(用户类型)
$usertype = isset(Yii::app()->user->usertype) ? Yii::app()->user->usertype : '';
注意:您不必编写文件来检查Yii用户状态。使用error_log("usertype = $usertype")
或打印视图中的State,而不是写入文件。