yii框架中的登录管理。您应该使用UserIdentity组件。您需要在组件文件夹下修改用户标识,如下所示。
假设我有一个类似以下的记录
用户-id-用户名-密码
登录时,我想用这个记录检查用户的x_username和x_password,我应该返回匹配的行。如何在CActiveRecord中执行此操作?
我尝试了以下
$user = Users::model()->find('username = :x_username AND password = :x_password');
但不起作用
如何做到这一点?
<?php
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$users=User::model()->find('LOWER(username)=?',array(strtolower($this->username)));
if(!isset($users[$this->username]))
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($users[$this->username]!==$this->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$user->id;
$this->username=$user->username;
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
有关
的信息,请参阅此链接尝试这个
$user = Users::model()->find('username = :x_username AND password =
:x_password',array(':x_username'=>'myname',':x_password'=>'mypassword'));
或
$user = Users::model()->find('username = :x_username AND password =
:x_password',array(':x_username'=>$myname,':x_password'=>$mypassword));