search
Homephp教程php手册yii实现创建验证码实例解析,yii验证码实例解析

yii实现创建验证码实例解析,yii验证码实例解析

本文以实例形式讲述了yii创建验证码的方法,具体步骤如下所示:

一、在SiteController action()下添加如下代码:

return array(
 // captcha action renders the CAPTCHA image displayed on the contact page
 'captcha'=>array(
 'class'=>'CCaptchaAction',
 'backColor'=>0xFFFFFF,
 ),
 // page action renders "static" pages stored under 'protected/views/site/pages'
 // They can be accessed via: index.php?r=site/page&view=FileName
 'page'=>array(
 'class'=>'CViewAction',
 ),
);

二、(1)在LoginForm model rules()下添加代码:

//captche class needed
array('verifyCode', 'captcha','allowEmpty'=>!CCaptcha::checkRequirements()),

    (2)LoginForm model下添加属性:

public $verifyCode;

三、在ContactForm model rules()下添加代码:

// verifyCode needs to be entered correctly
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),

四、在login view下添加代码:

<div class="row">
<&#63;php
echo $form->labelEx($model,'verifyCode');
&#63;>
<&#63;php
$this->widget('CCaptcha');
&#63;>
<&#63;php
echo $form->textField($model,'verifyCode');
&#63;>
<&#63;php
echo $form->error($model,'verifyCode');
&#63;>
</div>

本例代码仅为主要功能简述,读者还可以根据自身项目需求进一步完善该程序代码,使其功能更具实用性。

yii 中的验证码出不来是什问题

本文描述yii验证码的实现,仅仅是笔者应用的一个小例子,网上也有,总结一下,希望帮助到有需要的Yii爱好者。1、笔者用的是用户登录,所以在sitecontroller中加以下代码: public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
‘captcha’=>array(
‘class’=>’CCaptchaAction’,
‘backColor’=>0xFFFFFF, //背景颜色
‘minLength’=>4, //最短为4位
‘maxLength’=>4, //是长为4位
‘transparent’=>true, //显示为透明,当关闭该选项,才显示背景颜色
),
);
}
2、在表单文件中加如下代码(视图文件如login.php):

labelEx($model,’verifyCode’); ?>


widget(‘CCaptcha’); ?>
textField($model,’verifyCode’); ?>

输入验证码


error($model,’verifyCode’); ?>

3、在Loginform模型(LoginForm.php)中 加入如下代码,主要是添加属性字段,否则会报错(不存在的属性 ) public $username;
public $password;
public $verifyCode;
public $rememberMe;
private $_identity; 通过上面的操作实际上我们已经可以看到验证码了,但是在操作的时候我们会发现不输入验证码仍然可以,原因在于我们还没有指定验证是必须的,在LoginForm.php中加上array(‘verifyCode’,'required’)来指定必须,这时我们如果遗漏验证码,将会如下图所示:
本文描述yii验证码的实现,仅仅是笔者应用的一个小例子,网上也有,总结一下,希望帮助到有需要的Yii爱好者。1、笔者用的是用户登录,所以在sitecontroller中加以下代码......余下全文>>
 

yii自带验证码怎做?

共三步,分别controllers,models,views各一层添置一行代码即可实现
第一步在controllers添加
public function actions() {
return array( 'captcha' =>
array(
'class' => 'CCaptchaAction',
'backColor' => 0xF5F5F5,
'transparent'=>true,
'minLength'=>4, //最短为4位
'maxLength'=>8, //是长为4位
),
);
}
第二步在models添加如下:
public $verifyCode; //必须先定义
public function rules()
{
return array(
array('verifyCode', 'captcha'),
);
}
第三步在views添加如下(这里使用小物件)
beginWidget('CActiveForm')?>
widget('CCaptcha');?>
textField($model,'verifyCode'); ?>
error($model,'verifyCode'); ?>
endWidget(); ?>
?>
 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.