Yii method of using CAPTCHA verification Code, Yiicaptcha Verification Code
In this paper, the method of using CAPTCHA verification code for Yii is described. Share to everyone for your reference, as follows:
The detailed code can refer to: the example code of YII comes with The Post project, which has a contact form with the verification code.
1. Model:
Add a verification code to a property of Userlogin:
Class Userlogin extends cformmodel{public $username, public $password, public $rememberMe, public $verifyCode; Ction rules () { return Array ( //username and password are required Array (' username, Password,verifycode ', ' Required '), //RememberMe needs to is a Boolean array (' RememberMe ', ' Boolean '), //password needs to be Authen ticated Array (' Password ', ' Authenticate '), //Verifycode needs to be entered correctly array (' Verifycode ' , ' Captcha ', ' AllowEmpty ' =>! Ccaptcha::checkrequirements ()), ); }/** * declares attribute labels. */Public Function attributelabels () { return array (' rememberme ' =>yii::t (' user ', ' Remember me next time ') , ' username ' =>yii::t (' user ', "username or email"), ' Password ' =>yii::t (' user ', ' password '), ' Verifycode ' =>yii::t (' user ', ' Verification Code ') ; }}
2. Controller
Add a mapping action to the Logincontroller controller ccaptchaaction
Public function actions () {return array (//CAPTCHA action renders the CAPTCHA image displayed on the contact page ' Capt Cha ' =>array (' class ' = ' ccaptchaaction ', ' BackColor ' =>0xf4f4f4, ' padding ' =>0, ' height ' =>30, ' maxle Ngth ' =>4,),);} ublic function Actionlogin () {if (Yii::app ()->user->isguest) {$model =new userlogin; Collect user input data if (Isset ($_post[' userlogin ')) {$model->attributes=$_post[' userlogin '];//in this verification code if ($ This->createaction (' Captcha ')->validate ($model->verifycode, False)) {//validate user input and redirect to Previous page if valid if ($model->validate ()) {//admin login only if (Yii::app ()->getmodule (' user ')->i Sadmin () ==1) {$this->lastviset (); if (Strpos (Yii::app ()->user->returnurl, '/index.php ')!==false) $this->redirect (Yii::app () Controller->module->returnurl); else $this->redirect (Yii::app ()->user->returnurl); }else {//if No admin when login out $this->redirect (Yii::app ()->controller->module->logouturl); }}}else {//Prompt error $model->adderror (' Verifycode ', ' captcha incorrect '); }}//Display the login form $this->render ('/user/login ', Array (' model ' = $model)); } else $this->redirect (Yii::app ()->controller->module->returnurl);}
Before verifying the user name password, check the verification code:
if ($this->createaction (' Captcha ')->validate ($model->verifycode, false)) {
3. View
Display the Captcha picture in the view, enter the box
<?php $this->widget (' Ccaptcha ');?> <?php Echo Chtml::activetextfield ($model, ' Verifycode ', Array (' TabIndex ' =>1));?>
It is hoped that this article is helpful to the PHP program design based on YII framework.
Articles you may be interested in:
- Yii User Registration Form Validation instance
- PHP Yii Framework's form validation rules Daquan
- Yii Framework Form Form Usage example
- Yii Form Builder usage instances that do not rely on model
- Yii framework form model using and submitting form data as an array example
- A new method to implement pre-and post-logon in Yii
- Yii method for removing asterisks from required fields
http://www.bkjia.com/PHPjc/1085886.html www.bkjia.com true http://www.bkjia.com/PHPjc/1085886.html techarticle Yii uses the method of CAPTCHA verification code, YIICAPTCHA Verification Code This paper describes the method of using CAPTCHA verification code for YII. Share to everyone for reference, as follows: Detailed code can be ...