Method of Using Captcha verification code for Yii _ php instance

Source: Internet
Author: User
Tags contact form
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.

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.