Yii uses Captcha verification code, yiicaptcha verification code _ PHP Tutorial

Source: Internet
Author: User
Tags contact form
Yii uses the Captcha verification code, yiicaptcha verification code. Yii uses the Captcha verification code. This document describes how Yii uses the Captcha verification code. For your reference, refer to the following code: for details, refer to the Captcha verification code method of Yii and yiicaptcha verification code.

This example describes how Yii uses Captcha verification code. 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.

Articles you may be interested in:
  • Yii user registry ticket verification instance
  • PHP Yii Framework-table verification rules
  • Yii Framework form usage example
  • Example of Yii form builder usage independent of Model
  • Yii Framework form model usage and example of submitting form data in array form
  • Yii implements a new method for handling frontend and backend logins
  • Yii removes the star number in the required field

In this example, Yii uses Captcha verification code. For your reference, please refer to the following code...

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.