PHP development framework YiiFramework tutorial (20) UI component Captcha example

Source: Internet
Author: User
PHP development framework YiiFramework tutorial (20) UI component Captcha example

Captcha (a Completely Automated Turing test that distinguishes Computers from Humans-Completely automation Public Turing test to tell Computers and Humans Apart, CAPTCHA for short) is commonly known as a verification code, it is a public, fully automated program that distinguishes users from computers and people. In the CAPTCHA test, the server computer will automatically generate a question to be answered by the user. This question can be generated and judged by computers, but it must be answered by humans. Since computers cannot answer CAPTCHA's questions, users who answer questions can be considered human beings.

Yii Framework provides support for verification codes for CCaptcha and CCaptchaAction. Note that this function requires php gd extension support and can be queried through the Requirements application of Yii:

If the display is Warning, you can install the GD extension library and modify PHP. ini to enable this function.

CCaptcha also provides the CCaptcha: checkRequirements () method to check whether the GD Library is installed.

In this example, the Captcha function is added to the StarRating sample of the UI component by modifying the Yii Framework Development Tutorial (16). The user score is valid only when the entered verification code is correct, avoiding automatic scoring by machines.

First, modify DataModel, add a verifyCode attribute to store the verification code entered by the user, and add CCaptchaValidator verification to it.

Class

DataModel extends CFormModel
{
Public $ rating;
Public $ verifyCode;

Public function rules ()
{

Return array (
Array ('rating, verifycode', 'Safe '),
Array ('verifycode', 'captcha ',
'Allowempty '=>! CCaptcha: checkRequirements ()),
);

}
}

Modify SiteController and add the actions method. the Captcha component uses CCaptchaAction by default, and its default ID is captcha.

Public function actions ()
{
Return array (
'Captcha '=> array (
'Class' => 'ccaptchaaction ',
));
} You can add the Captcha component to the View:


BeginWidget ('cactiveform');?>

ErrorSummary ($ model);?>


Widget ('cstarrating', array (
'Model' => $ model,
'Attribute' => 'rating ',
'Name' => 'rating ',
'Value' => 3,
);?>


Label ($ model, 'verifycode')?>


Widget ('ccaptcha ');?>


TextField ($ model, 'verifycode')?>




 
EndWidget ();?>


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.