Yii verification code usage and verification process

Source: Internet
Author: User
: This article describes how to use and verify the yii verification code. if you are interested in the PHP Tutorial, refer to it. To implement this process, several steps are required.

The first step is the controller operation.

Add the following code to the controller to be operated:

Public function actions () {return array (// captcha action renders the CAPTCHA image displayed on the contact page 'captcha '=> array ('class' => 'ccaptchaaction ', 'backcolor' => 0 xFFFFFF, 'maxlength' => '8', // A maximum of several characters 'minlength' => '7' can be generated ', // generate at least a few characters 'height' => '40', 'width' => '000000',),);} public function accessRules () {return array ('allow', 'actions' => array ('captcha '), 'users' => array ('*'),),);}

Step 2: view operations

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

Widget ('ccaptcha ', array ('showrefreshwith' => true, 'clickableimage' => false, 'buttonlab' => 'refresh CAPTCHA ', 'imageoptions' => array ('alt' => 'Click to change the Tues', 'title' => 'Click to change the Tues', 'Style' => 'cursor: pointer ', 'padding' => '10');?>


Step 3 is LoginForm operations

 RequireD, * and password needs to be authenticated. */public function rules () {return array (// username and password areRequireD // array ('username, password ','RequireD'), array ('username ','RequireD', 'message' => 'Login account cannot be blank '), array ('password ','RequireD', 'message' => 'Password cannot be blank '), array ('verifycode ','RequireD', 'message' => 'verification code cannot be blank '), 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 '=> 'Remember me next time', 'verifycode' => 'captcha ');}/*** Authenticates the password. * This is the 'authenticate' validator as declared in rules (). */public funct Ion authenticate ($ attribute, $ params) {if (! $ This-> hasErrors () {$ this-> _ identity = new UserIdentity ($ this-> username, $ this-> password); if (! $ This-> _ identity-> authenticate () $ this-> addError ('password', 'account or password error. ') ;}} public function validateVerifyCode ($ verifyCode) {if (strtolower ($ this-> verifyCode) === strtolower ($ verifyCode) {return true ;} else {$ this-> addError ('verifycode', 'verification code error. ');}/*** Logs in the user using the given username and password in the model. * @ return boolean whether login is successful */public function login () {if ($ this-> _ ide Ntity = 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 daysYii: app ()-> user-> login ($ this-> _ identity, $ duration); return true ;} else {return false ;}}}

Step 4: implement the verification process. check a specific method I have written in the third part.
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(CController::createUrl('default/index'));}}$this->render('login',array('model'=>$model));}

The above describes the yii verification code use and verification process, including The require and ajax content, and hope to be helpful to friends who are interested in PHP tutorials.

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.