南龙 2012-02-11 14:59:31 4070次浏览 1条回复 0 0 0

//控制方法

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;
	}
}
  • 回复于 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);
    }
    
您需要登录后才可以回复。登录 | 立即注册