Home >Backend Development >PHP Tutorial >How to load the built-in verification code in yii_PHP tutorial

How to load the built-in verification code in yii_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:03:531218browse

Yii's method of loading its own verification code

Yii's source code package has related classes with its own verification code, so there is no need to load external ones when using the verification code The CAPTCHA class comes to help. The following article will introduce how to load the verification code function that comes with Yii in the project.

It is divided into three steps:

(1) Add the following code in 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位不限制
		)
	);
}

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

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

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

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

Articles you may be interested in

  • Solving the problem that the verification code that comes with Yii does not refresh automatically with page refresh
  • Understanding of Yii framework Yiiapp()
  • Yii framework module development analysis
  • Yii rules common verification rules memo
  • Yii rules verification example summary
  • Yii CDbCriteria summary of common methods
  • Yii gets the current controller name and action name
  • Yii database addition, modification, deletion related operations summary

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1076538.htmlTechArticleYii's method of loading its own verification code. Yii's source code package contains related classes with its own verification code. Therefore, there is no need to load external verification code classes to help when using verification codes. Below...
Statement:
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