in the process of Web development , Verification code is often used to prevent the robot from constantly submitting data and causing the website to be paralyzed . Yii provides a verification code plug- in, that is , Captcha.
The first step:
using in Projects Captcha some of the following settings are required : in the Controller Add method in Actions
Public function actions () { return array (' captcha ' = = Array ( ' class ' = ' ccaptchaaction ', c14/> ' minLength ' + 1, ' maxLength ' and ' 5, ' BackColor ' + 0xFFFFFF, ' width ' = +, ' Hei Ght ' = + ) ; }
Also , you need to add captacha to accessrules to allow all users to access the method . as follows
Add a property:
public $verifyCode; Array (' Verifycode ', ' captcha ', ' AllowEmpty ' =>! Ccaptcha::checkrequirements ()), Array (' Captcha ', ' captcha ', ' AllowEmpty ' =>!extension_loaded (' gd '), ' on ' = ' Login '),//Added at the end of the rules array:
Second, add the following code to your view
<?php $this->widget (' Ccaptcha ')?>//The following can be clicked on the image for verification code <div><?php $this->widget (' Ccaptcha ', Array (' Showrefreshbutton ' =>false, ' clickableimage ' =>true, ' imageoptions ' =>array (' alt ' = = ') Click Change Image ', ' title ' = ' Click to change image ', ' style ' = ' cursor:pointer '));?></div>
Thirdly, we need to add a Verifycode attribute to our form model to hold the user input verification code, and then pass the captcha Validator to verify the accuracy of the verification code entered by the user.
Public $verifyCode, and add the following public function rules in the rules () { return array (' Verifycode ', ' captcha ', ' on ' = ' Login ', ' allowempty ' = '!extension_loaded (' gd '), ...
The reason why the captcha is not refreshed:
Background login Call Verification code, click the Verification code does not refresh, no response (all verification code configuration, parameters are correct). Find the wrong time to find that can refresh the verification code of the page than can not refresh the verification code of the page more than a section of JS code, to handle the Click Verification Code Refresh event.
Why is there a piece of code missing? The original is because the controller called renderpartial and render respectively. Let's talk about the difference:
When rendering the page output.
Render outputs the contents of the parent template, and embeds the rendered content into the parent template.
RenderPartial does not output the contents of the parent template. The output is only for the local content of this rendering.
There is also an important difference:
The processoutput ($output) function is executed by default inside the render function, which registers the components, such as CTreeView , inside the Cclientscript inside the desired script to render the output.
renderpartial () does not automatically render the output client script by default, it needs to specify the parameters before output:
RenderPartial ($view, $data =null, $return =false, $processOutput =false)
Specifies that processoutput is true .
Just like the problem we encountered, when using renderpartial , the page did not output the code to refresh the js. So there's no response .
Yii Verification Code CAPTCHA use and why verification code does not refresh the problem