This article mainly introduces how Yii uses Captcha verification code, and analyzes the three-layer MVC implementation skills of Yii using Captcha verification code in combination with examples, for more information about how Yii uses Captcha verification codes, see the following example. We will share this with you for your reference. The details are as follows:
For details about the code, refer to the post project with the sample code provided by yii. the verification code is used in a contact form.
1. Model:
Add the verification code to an attribute of UserLogin:
class UserLogin extends CFormModel{ public $username; public $password; public $rememberMe; public $verifyCode; public function rules() { return array( // username and password are required array('username, password,verifyCode', 'required'), // rememberMe needs to be a boolean array('rememberMe', 'boolean'), // password needs to be authenticated 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 ing action to the LoginController CCaptchaAction
Public function actions () {return array (// captcha action renders the CAPTCHA image displayed on the contact page 'captcha '=> array ('class' => 'ccaptchaaction ', 'backcolor' => 0xf4f4f4, 'padding' => 0, 'height' => 30, 'maxlength' => 4,),);} ublic function actionLogin () {if (Yii: app ()-> user-> isGuest) {$ model = new UserLogin; // collect user input data if (isset ($ _ POST ['userlogin']) {$ model-> attributes = $ _ PO ST ['userlogin']; // Check the verification code here 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')-> isAdmin () = 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 {// error $ model-> addError ('verifycode', 'incorrect verification code ');}} // display the login form $ this-> render ('/user/login', array ('model' => $ model ));} else $ this-> redirect (Yii: app ()-> controller-> module-> returnUrl );}
Check the verification code before verifying the user name and password:
if($this->createAction('captcha')->validate($model->verifyCode, false)){
3. view
Display the verification code image in the view. input box
<?php $this->widget('CCaptcha'); ?> <?php echo CHtml::activeTextField($model,'verifyCode',array('tabindex'=>1)); ?>
I hope this article will help you design PHP programs based on the Yii Framework.