PHP Development Framework Yii Framework tutorial (UI component Captcha example)

Source: Internet
Author: User
Captcha (automatic distinction between computer and human Turing Test-completely Automated public Turing test to tell Computers and humans Apart, abbreviated as Captcha) commonly known as CAPTCHA, is a public automatic program that distinguishes users from computers and people. In the CAPTCHA test, a computer that acts as a server automatically generates an issue that is answered by the user. This problem can be generated and judged by a computer, but only humans can answer it. Since the computer is unable to answer the CAPTCHA question, the user who answers the question can be considered human.

The YII framework provides classes Ccaptcha and ccaptchaaction to support verification codes, note that this feature requires PHPGD extension support and can be queried through Yii's requirements application:

If warning is displayed, you can open the feature by installing the GD extension Library and modifying php.ini.

Ccaptcha also provides method ccaptcha::checkrequirements () to detect if the GD library is installed.

This example adds CAPTCHA functionality to the Yii Framework Development Tutorial (starrating) UI component by modifying it, and the user score is valid only if the verification code entered is correct, avoiding automatic scoring by the machine.

The first is to modify the Datamodel, add a property verifycode to hold the user input verification code, and add Ccaptchavalidator validation.

Classdatamodel extends Cformmodel{public $rating;p ublic $verifyCode;p ublic function rules () {return Array (array (' Rating,verifycode ', ' safe '), Array (' Verifycode ', ' captcha ', ' AllowEmpty ' =>! Ccaptcha::checkrequirements ()),);}}

Then modify the Sitecontroller, add the actions method, and the Captcha component defaults to ccaptchaaction with the default ID of CAPTCHA.

Public function actions () {return array (' Captcha ' =>array (' class ' = ' Ccaptchaaction ',)});

You can add the CAPTCHA component to the view below:

Beginwidget (' Cactiveform ');? >errorsummary ($model);? >widget (' Cstarrating ', Array (' model ' = $model, ' Attribute ' = ' rating ', ' name ' = ' rating ', ' value ' =>3, '); >label ($model, ' Verifycode ')?> widget (' Ccaptcha ');  ? >textfield ($model, ' Verifycode ')? >endwidget ();?>

The above is the content of the PHP Development Framework Yii Framework Tutorial (CAPTCHA) UI component sample, more about topic.alibabacloud.com (www.php.cn)!

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