程序判断逻辑贴出来看下!!

解决方案 »

  1.   

    控制器:
    <?phpclass LoginController extends Controller{    /*
         * 登陆模板*/
        public function actionIndex(){        $login_model = new LoginForm();
               
            if(isset($_POST['LoginForm'])){            $login_model->attributes = $_POST['LoginForm'] ;            if($login_model->validate() && $login_model->login()){                $this->redirect(Yii::app()->homeUrl);
                }
            }        $this->render('index',array('login_model'=>$login_model));
        
        }   
        /*
         * 验证码
         * */
        public function actions(){
            return array(
                'captcha'=>array(
                    'class'=>'CCaptchaAction',
                    'height'=>40,
                    'width'=>80,
                    'minLength'=>4,
                    'maxLength'=>4,
                ),
            );
        }    /*
         * 登出*/
        public function actionLogout(){
            Yii::app()->user->logout();
            $this->redirect($this->createUrl('index/index'));
        }}数据模型:
    <?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 $email;
                public $vcode; 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(
    // username and password are required

    array('password', 'required','message'=>'密码不能为空'),
    array('email', 'required','message'=>'电邮不能为空'),
    array('vcode', 'required','message'=>'验证码不能为空'),
    //array('vcode','captcha','message'=>'请输入正确的验证码'),
    // password needs to be authenticated
    array('password', 'authenticate'), );
    } /**
     * Declares attribute labels.
     */
    public function attributeLabels()
    {
    return array(

            'email'=>'email:',
            'password'=>'密&nbsp;码:',
            'vcode'=>'验证码:',
    );
    } /**
     * 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->email,$this->password);
    if(!$this->_identity->authenticate())
    $this->addError('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->email,$this->password);
                $this->_identity->authenticate();
            
            }
            if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
            {
                $duration = 0; 
                Yii::app()->user->login($this->_identity,$duration);
                return true;
            }
            else
                return false;
    }
    }