????????public?function?actionLogin()
????????{
????????????$user_login?=?new?LoginForm();?//LoginForm?是?YII自帶的一個(gè)文件?需要配置
????????????if(isset($_POST['LoginForm']))
????????????{
????????????????$user_login->attributes=$_POST['LoginForm'];?
????????????????if($user_login->validate()?&&?$user_login->login())?//validate?驗(yàn)證??login?設(shè)置session
????????????????{
????????????????????$this->redirect('index.php');
????????????????}
????????????????
????????????}
????????????$this->render('login',array('user_login'=>$user_login));
????????}LoginForm 文件
public?function?authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$this->_identity=new?UserIdentity($this->username,$this->password);?//需要到?UserIdentity中配置
if(!$this->_identity->authenticate())
$this->addError('password','用戶名或密碼錯(cuò)誤');
}
}在 components 中 UserIdentity
public?function?authenticate()
{
????????????????$user_model?=?User::model()->find('username=:name',array(':name'=>$this->username));
????????????????
????????????????if($user_model?===?NULL)
????????????????????$this->errorCode=self::ERROR_USERNAME_INVALID;
????????????????elseif($user_model->password?!==?$this->password)
????????????????????$this->errorCode=self::ERROR_PASSWORD_INVALID;
????????????????else
????????????????????$this->errorCode=self::ERROR_NONE;
????????????????return?!$this->errorCode;
}下面 就 是簡(jiǎn)單的了
public?function?rules()
{
return?array(
//?username?and?password?are?required
array('username',?'required'?,'message'=>'用戶名必填'),
????????????????????????
????????????????????????array('password',?'required'?,'message'=>'密碼必填'),
//?rememberMe?needs?to?be?a?boolean
array('rememberMe',?'boolean'),
//?password?needs?to?be?authenticated
array('password',?'authenticate'),
);
}
/**
?*?Declares?attribute?labels.
?*/
public?function?attributeLabels()
{
return?array(
'rememberMe'=>'記住我',
????????????????????????'username'=>'用戶名',
????????????????????????'password'=>'密碼',
);
}
/**
?*?Authenticates?the?password.
?*?This?is?the?'authenticate'?validator?as?declared?in?rules().
?*/
public?function?authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$this->_identity=new?UserIdentity($this->username,$this->password);?//需要到?UserIdentity中配置
if(!$this->_identity->authenticate())
$this->addError('password','用戶名或密碼錯(cuò)誤');
}
}




