PHP Development Framework Yii Framework Tutorial (a) UI component custom Captcha sample

Source: Internet
Author: User
Tags gettext rand yii

Yii built-in CAPTCHA can basically meet most of the requirements, if you have special requirements for the verification code, you can customize the CAPTCHA, this is mainly through the expansion of ccaptchaaction to achieve, this example to customize a validation code function, randomly generated within 10 of addition and subtraction, The user needs to calculate the correct results to pass the validation.
This example is based on the previous example of the Yii Framework Development Tutorial (a) UI component Captcha sample, which is modified as follows

First in the Protected/components directory to create a mathcaptchaaction, overload Generateverifycode, RenderImage and other methods:

Class Mathcaptchaaction 

extends ccaptchaaction      
{      

    protected function Generateverifycode ()      
    {      
        return Mt_rand ((int) $this->minlength,      
            (int) $this->maxlength);      
    }      

    Public Function RenderImage ($code)      
    {      
        parent::renderimage ($this->gettext ($code));      
    }      

    protected function GetText ($code)      
    {      
        $code = (int) $code;      
        $rand =mt_rand (1, $code-1);      
        $op =mt_rand (0,1);      
        if ($op)      
        {return      

            $code-$rand. '+' . $rand;      

        } else 
        {return      
            $code + $rand. '-' . $rand;      
        }}}      

Then modify Sitecontroller's rules to use the newly created mathcaptchaaction

The Public Function actions () 
{return
    array (' Captcha ' =>array) ('
                class ' => ' mathcaptchaaction ')
                ' minlength ' => 1,
                ' maxLength ' =>)
        ;
}

This example downloads: Http://www.imobilebbs.com/download/yii/CustomCaptchaDemo.zip

See a full set of tutorials: http://www.bianceng.cn/webkf/PHP/201301/35265.htm

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.