Methods of using CAPTCHA verification code for YII

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

The above describes the use of Yii Captcha verification code method, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.