Use of Yii Verification code and verification process

Source: Internet
Author: User
The use and verification process of Yii Verification code

If you want to implement this process, you need a few steps.

The first step is the controller's operation.

Add the following code to the controller you want to manipulate:

Public function actions () {return array (//CAPTCHA action renders the CAPTCHA image displayed on the contact page ' Captcha ' =>array (' class ' = ' ccaptchaaction ', ' BackColor ' =>0xffffff, ' maxLength ' = ' 8 ',//generate up to a few characters ' minLength ' = > ' 7 ',//at least generate a few characters ' height ' = ' + ', ' width ' = ' 230 ',); }public function Accessrules () {return array (' Allow ', ' actions ' =>array (' captcha '), ' Users ' =>array (' * '), ,);}

  

The second step is the operation of the view

Add the following code where you want the verification code to appear:

 
  Widget (' Ccaptcha ', Array (' Showrefreshbutton ' =>true, ' clickableimage ' =>false, ' buttonlabel ' = ' refresh Captcha ', ' Imageoptions ' =>array (' alt ' = ' Click to change ', ' title ' = ' Click to change ', ' style ' = ' cursor:pointer ', ' padding ' = ' 10 ')) ;?>


The third step is loginform operation.

 ' Login account cannot be empty '), array (' Password ', ' Required ', ' message ' = ' password cannot be null '), array (' Verifycode ', ' Required ', ' message ' = = ') Verification code cannot be empty '), array (' Verifycode ', ' captcha ', ' on ' = ' login ', ' AllowEmpty ' =>! Yii::app ()->admin->isguest),//RememberMe needs to be a booleanarray (' RememberMe ', ' Boolean '),//password needs to Be Authenticatedarray (' password ', ' Authenticate '),);} /*** declares attribute labels.*/public function Attributelabels () {return array (' rememberme ' = ' next time remember me ', ' Verifycode ' = ' Verification Code ');} /*** authenticates the password.* this is the ' authenticate ' validator as declared in rules (). */public function Authentica Te ($attribute, $params) {if (! $this->haserrors ()) {$this->_identity=new useridentity ($this->username,$ This->password); if (! $this->_identity->authenticate ()) $this->adderror (' Password ', ' Incorrect account or password ');}} Public Function Validateverifycode ($verifyCode) {if (Strtolower ($this->verifycode) = = = Strtolower ($verifyCode)) { return true;} else{$this->adderror (' Verifycode ', ' captcha error. ');}} /Logs in the user using the given username and password in the model.* @return Boolean whether login is Successful*/pub Lic 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; /Daysyii::app ()->user->login ($this->_identity, $duration); return true;} Else{return false;}}}

  

The fourth step, the implementation of the verification process, then the specific view of my own write a way, in the third part has been written well
Validateverifycode, you can call it in the controller.

My call is as follows:

Public Function Actionlogin () {$model =new loginform;if (isset ($_post[' ajax]) && $_post[' Ajax ']=== ' Login-form ') {echo cactiveform::validate ($model); Yii::app ()->end ();} if (Isset ($_post[' LoginForm '))) {$model->attributes=$_post[' loginform '];//validate user input and redirect to the Previous page if Validif ($model->validate () && $model->validateverifycode ($this->createaction (' Captcha ')->getverifycode ()) && $model->login ()) {$this->redirect (' Default /index '));}} $this->render (' login ', array (' model ' = $model));}

  

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.