Home > Backend Development > PHP Tutorial > How to load the verification code function that comes with Yii

How to load the verification code function that comes with Yii

小云云
Release: 2023-03-17 15:48:02
Original
1459 people have browsed it

Introduction: Yii Framework is a component-based, high-performance PHP framework for developing large-scale web applications. Yii provides almost everything needed for today's Web 2.0 application development. Yii is one of the most efficient PHP frameworks. Yii's source code package comes with related classes for verification codes, so there is no need to load external verification code classes to help when using verification codes. The following article will introduce how to load the verification code function that comes with Yii in the project.

Specifically divided into three steps:

(1) Add the following code to the controllers file that needs to load the verification code:

public function actions(){    
return array(    
'captcha'=> array(     
'class'=>'Captcha',    
 'width'=>65, //默认120    
 'height'=>25, //默认50    
'padding'=>0, //文字周边填充大小    
 'backColor'=>0xFFFFFF, //背景颜色    
 'foreColor'=>0x2040A0, //字体颜色    
'minLength'=>4, //设置最短为4位    
'maxLength'=>4, //设置最长为4位,生成的code在6-7直接rand了    
'transparent'=>false, //显示为透明,默认中可以看到为false    
 'offset'=>1, //设置字符偏移量    
'testLimit'=>0 //限制相同验证码出现的次数,0位不限制    
 )    
);    
}
Copy after login

(2) In the models corresponding to the controllers file Add the following code to the file:

<?php    
 ......    
public $verifyCode;//必须先定义    
 ......    
public function rules(){    
return array(    
......    
//注意这里的&#39;on&#39;=>&#39;login&#39;,即action=login的时候显示    
array(&#39;verifyCode&#39;,&#39;captcha&#39;,&#39;on&#39;=>&#39;login&#39;,&#39;allowEmpty&#39;=>!extension_loaded(&#39;gd&#39;)),    
 );    
}    
......    
?>
Copy after login

(3) Add the following code to the views page that needs to load the verification code:

<?php    
$this->widget(&#39;CCaptcha&#39;,    
array(    
 &#39;showRefreshButton&#39;=>false,    
 &#39;clickableImage&#39;=>true,    
 &#39;imageOptions&#39;=>array(    
&#39;alt&#39;=>&#39;点击换图&#39;,    
&#39;title&#39;=>&#39;点击换图&#39;,    
&#39;id&#39;=>&#39;checkcodeImg&#39;,    
&#39;style&#39;=>&#39;cursor:pointer;&#39;    
 )  ) );
?>
Copy after login

We used three steps to complete how to load Yii in the project Have you learned about the verification code function? Collect it quickly.

Related recommendations:

Yii framework framework module development

Summary of database query operations of PHP Yii framework

PHP—yii framework cache knowledge collection

The above is the detailed content of How to load the verification code function that comes with Yii. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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