Yii Implementation Create Verification Code instance resolution, YII verification Code Instance parsing _php tutorial

Source: Internet
Author: User

Yii implementation to create verification code instance resolution, YII verification Code Instance parsing


In this paper, we describe the method of Yii to create a verification code in the form of an example, as follows:

First, add the following code under Sitecontroller action ():

return Array (//CAPTCHA action renders the CAPTCHA image displayed on the contact page ' captcha ' =>array (' class ' = = ') Ccaptchaaction ', ' BackColor ' =>0xffffff,),//page action renders "static" pages stored under ' Protected/views/site/pa Ges '//They can be accessed via:index.php?r=site/page&view=filename ' page ' =>array (' class ' = ' cviewaction ',) ,);

Second, (1) Add the code under LoginForm Model Rules ():

Captche class Neededarray (' Verifycode ', ' captcha ', ' AllowEmpty ' =>! Ccaptcha::checkrequirements ()),

(2) Add properties under LoginForm Model:

Public $verifyCode;

Third, add the code under Contactform Model Rules ():

Verifycode needs to be entered Correctlyarray (' Verifycode ', ' captcha ', ' AllowEmpty ' =>! Ccaptcha::checkrequirements ()),

Iv. Add the code under Login view:

<?phpecho $form->labelex ($model, ' verifycode ');? ><?php$this->widget (' Ccaptcha ');? ><?phpecho $form->textfield ($model, ' verifycode ');? ><?phpecho $form->error ($model, ' verifycode ');? >

This example code is only the main function of the brief, the reader can also be based on their own project requirements to further refine the program code, so that its function more practical.


What is wrong with the verification code in YII?

This article describes the implementation of YII verification code, is only a small example of the application of the author, on-line also has, summed up, hope to help the needs of yii enthusiasts. 1, the author uses the user login, so in the Sitecontroller code: Public Function Actions ()
{
Return Array (
Captcha action renders the CAPTCHA image displayed on the contact page
' Captcha ' =>array (
' Class ' = ' ccaptchaaction ',
' BackColor ' =>0xffffff,//Background color
' MinLength ' =>4,//Minimum 4-bit
' MaxLength ' =>4,//is 4 bits long
' Transparent ' =>true,//display as transparent, the background color is displayed when this option is turned off
),
);
}
2. Add the following code to the form file (view files such as login.php):
< div>
< php echo $form->labelex ($model, ' Verifycode ');?>
< div>
< PHP $this->widget (' Ccaptcha ');?>
< php echo $form->textfield ($model, ' Verifycode ');?>
</div>
< div> input Verification code
</div>
< php echo $form->error ($model, ' Verifycode ');?>
</div>
3, in the LoginForm model (loginform.php) to add the following code, mainly the addition of attribute fields, otherwise will error (non-existent properties) public $username;
Public $password;
Public $verifyCode;
Public $rememberMe;
Private $_identity; Through the above operation we can actually see the verification code, but in the operation of the time we will find not to enter the verification code is still possible, because we have not specified the validation is necessary, in the loginform.php Add array (' Verifycode ', ' Required ') to specify the must, when we omit the verification code, will be as shown:
This article describes the implementation of YII verification code, is only a small example of the application of the author, on-line also has, summed up, hope to help the needs of yii enthusiasts. 1, the author uses the user login, so in the Sitecontroller code ... Remaining full text >>

What does Yii do with its own verification code?

A total of three steps, respectively controllers,models,views each layer to add a line of code to achieve
the first step in controllers
Public function actions () {
Return Array (' captcha ' = =
Array (
' class ' = ' ccaptchaaction ',
' backColor ' = = 0xf5f5f5,
' Transparent ' =>true,
' minLength ' =>4,//Minimum 4-bit
' maxLength ' =>8,//is 4 bits long
),
);
}
The second step is added as follows in models:
!--? php
public $verifyCode;//must first define
Public function rules ()
{
Return Array (
Array (' Verifycode ', ' captcha '),
);
}
The third step in the views are added as follows (use small objects here)
!--? php $form = $this--->beginwidget (' cactiveform '),
!--? PHP $this--- Widget (' Ccaptcha ');?
!--? php echo $form--->textfield ($model, ' Verifycode ');
!--? php echo $form--->error ($model, '
!--? php $this--->endwidget (); verifycode;
;
 

http://www.bkjia.com/PHPjc/854351.html www.bkjia.com true http://www.bkjia.com/PHPjc/854351.html techarticle Yii Implementation to create verification code instance resolution, YII verification Code Instance parsing this article describes the method of Yii to create the verification code in the instance form, the following steps are as follows: First, in Sitecontroller acti ...

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