为什么注册并且直接登录都无法成功,也不显示错误信息 [ 新手入门 ]
//控制方法
public function actionCheck()
{
$Model=new Reg();
//ajax验证表单
if(isset($_POST['ajax']) && $_POST['ajax']==='Regform')
{
echo CActiveForm::validate($Model);
Yii::app()->end();
}
if(isset($_POST['Reg']))
{
$Model->attributes=$_POST['Reg'];
if($Model->validate())
{
//添加注册用户
$user=new User();
$_POST['Reg']['password']=md5($_POST['Reg']['password']);
$user->attributes=$_POST['Reg'];
$user->session=session_id();
if($user->save())
{
$login=new LoginForm();
$login->username=$_POST['Reg']['username'];
$login->password=$_POST['Reg']['password'];
$login->rememberMe=true;
if($login->login()) //到这里一直返回错误。
{
$this->redirect(Yii::app()->baseUrl);
}else{
var_dump($login->getErrors());
}
//$this->redirect(Yii::app()->baseUrl);
}
}
}
}
<?php
//这里是登录验证
/**
* LoginForm class.
* LoginForm is the data structure for keeping
* user login form data. It is used by the 'login' action of 'SiteController'.
*/
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;
private $_identity;
/**
* Declares the validation rules.
* The rules state that username and password are required,
* and password needs to be authenticated.
*/
public function rules()
{
return array(
array('user, password', 'required'),
array('rememberMe', 'boolean'),
array('password', 'authenticate'),
);
}
/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
'rememberMe'=>'Remember me next time',
);
}
/**
* 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);
if(!$this->_identity->authenticate())
$this->addError('password','Incorrect username or password.');
}
}
/**
* Logs in the user using the given username and password in the model.
* @return boolean whether login is successful
*/
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->username,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
else
return false;
}
}
共 1 条回复
-
francis.xia 回复于 2012-02-17 16:01 举报
public static function auto_login($email,$pwd) { $identity=new UserIdentity($email,$pwd); $identity->authenticate(); if($identity->errorCode!=UserIdentity::ERROR_NONE) { Yii::app()->user->loginRequired(); } Yii::app()->user->login($identity); }
南龙
注册时间:2012-01-05
最后登录:1970-01-01
在线时长:0小时0分
最后登录:1970-01-01
在线时长:0小时0分
- 粉丝0
- 金钱135
- 威望0
- 积分135