YII 框架 第七天 登陆

        public function actionLogin()
        {
            $user_login = new LoginForm(); //LoginForm 是 YII自带的一个文件 需要配置
            if(isset($_POST['LoginForm']))
            {
                $user_login->attributes=$_POST['LoginForm']; 
                if($user_login->validate() && $user_login->login()) //validate 验证  login 设置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','用户名或密码错误');
		}
	}

在 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;
	}



下面 就 是简单的了



	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','用户名或密码错误');
		}
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值