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 ();?>