Home > PHP Framework > YII > How to use yii's own verification code

How to use yii's own verification code

爱喝马黛茶的安东尼
Release: 2019-11-09 10:39:15
Original
2089 people have browsed it

How to use yii's own verification code

It is divided into three aspects: controller configuration, model rules configuration and view configuration.

Step one: Controller configuration

Configure the following code in actions, and the request verification code link corresponds to "controller/captcha".

'captcha' => [
         'class' => 'yii\captcha\CaptchaAction',
    //     'backColor'=>0x000000,//背景颜色
         'maxLength' => 4, //最大显示个数
         'minLength' => 4,//最少显示个数
         'padding' => 5,//间距
         'height'=>45,//高度
         'width' => 75,  //宽度
      //   'foreColor'=>0xffffff,     //字体颜色
         'offset'=>4,
 ],
Copy after login

The second step: model rules configuration, that is, verify the configuration.

First declare the public variables

public $verifyCode
Copy after login

Secondly configure the verification in the rules method

['verifyCode', 'captcha','captchaAction'=>'user/captcha','on' => ['login']]
Copy after login

Add "captchaAction" in this configuration Configuration, that is, the routing of the controller. If not configured, the site/captcha routing will be used. This is the routing of the framework example.

Step Three: Configuration in the View

Add the following code to ActiveForm:

<?= $form->field($model,&#39;verifyCode&#39;)->widget(
    Captcha::className(),
    [
    &#39;template&#39; => &#39;{image}{input}&#39;,
    &#39;captchaAction&#39;=>&#39;user/captcha&#39;, // 此注意要使用自己配置的那个控制器验证码路由
    &#39;imageOptions&#39;=>[ &#39;style&#39;=>&#39;cursor:pointer;width:77px&#39;]
    ]
    )
Copy after login

Finally there is a huge pit problem : Due to the use of some newline characters or spaces under Windows during the encoding process, unknown output is caused and the verification code image cannot be generated normally.

This is the need to add the following code to the renderImageByGD method in the CaptchaAction class:

imagecolordeallocate($image, $foreColor);
 
ob_clean(); // 此行为插入代码,目的是清空缓存区为输出图片提供干净空间。
ob_start();
imagepng($image);
imagedestroy($image);
Copy after login

php Chinese website, a large number of free yii introductory tutorials, welcome to learn online!

The above is the detailed content of How to use yii's own verification code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
yii
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template