yii 验证码的使用和验证过程

如果要实现这个过程的话,需要几个步骤

第一步就是controller的操作

在要操作的控制器中添加如下代码:

public function actions(){
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
‘captcha‘=>array(
‘class‘=>‘CCaptchaAction‘,
‘backColor‘=>0xFFFFFF,
‘maxLength‘=>‘8‘, // 最多生成几个字符
‘minLength‘=>‘7‘, // 最少生成几个字符
‘height‘=>‘40‘,
‘width‘=>‘230‘,
),
); 

}

public function accessRules(){
return array(
array(‘allow‘,
‘actions‘=>array(‘captcha‘),
‘users‘=>array(‘*‘),
),
);
}

  

第二步就是view的操作

在要显示验证码的地方添加如下代码:

<div class="control-group">
<?php $this->widget(‘CCaptcha‘,array(
‘showRefreshButton‘=>true,
‘clickableImage‘=>false,
‘buttonLabel‘=>‘刷新验证码‘,
‘imageOptions‘=>array(
‘alt‘=>‘点击换图‘,
‘title‘=>‘点击换图‘,
‘style‘=>‘cursor:pointer‘,
‘padding‘=>‘10‘)
)); ?>
</div>

第三步就是LoginForm的操作

<?php

/**
* LoginForm class.
* LoginForm is the data structure for keeping
* user login form data. It is used by the ‘login‘ action of ‘SiteController‘.
*/
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;
public $verifyCode;
private $_identity;

/**
* Declares the validation rules.
* The rules state that username and password are required,
* and password needs to be authenticated.
*/
public function rules(){
return array(
// username and password are required
//	array(‘username, password‘, ‘required‘),
array(‘username‘,‘required‘,‘message‘=>‘登录帐号不能为空‘),
array(‘password‘,‘required‘,‘message‘=>‘密码不能为空‘),
array(‘verifyCode‘,‘required‘,‘message‘=>‘验证码不能为空‘),
array(‘verifyCode‘,‘captcha‘, ‘on‘=>‘login‘,‘allowEmpty‘=>!Yii::app()->admin->isGuest),
// rememberMe needs to be a boolean
array(‘rememberMe‘, ‘boolean‘),
// password needs to be authenticated
array(‘password‘, ‘authenticate‘),
);
}

/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
‘rememberMe‘=>‘下次记住我‘,
‘verifyCode‘ =>‘验证码‘
);
}

/**
* Authenticates the password.
* This is the ‘authenticate‘ validator as declared in rules().
*/
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$this->_identity=new UserIdentity($this->username,$this->password);
if(!$this->_identity->authenticate())
$this->addError(‘password‘,‘帐号或密码错误.‘);
}
}

public function validateVerifyCode($verifyCode){
if(strtolower($this->verifyCode) === strtolower($verifyCode)){
return true;
}else{
$this->addError(‘verifyCode‘,‘验证码错误.‘);
}
}

/**
* Logs in the user using the given username and password in the model.
* @return boolean whether login is successful
*/
public function login(){
if($this->_identity===null){
$this->_identity=new UserIdentity($this->username,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE){
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}else{
return false;
}
}
}

  

第四步,实现验证的过程,那么具体的查看我自己的写的一个方式,在第三部已经写好了
validateVerifyCode就是啦,可以在controller里面调用

我的调用如下:

public function actionLogin(){
$model=new LoginForm;
if(isset($_POST[‘ajax‘]) && $_POST[‘ajax‘]===‘login-form‘){
echo CActiveForm::validate($model);
Yii::app()->end();
}

if(isset($_POST[‘LoginForm‘])){
$model->attributes=$_POST[‘LoginForm‘];
// validate user input and redirect to the previous page if valid
if($model->validate() &&
$model->validateVerifyCode($this->createAction(‘captcha‘)->getVerifyCode()) &&
$model->login()){
$this->redirect(CController::createUrl(‘default/index‘));
}

}
$this->render(‘login‘,array(‘model‘=>$model));
}

  

时间: 2024-08-11 07:39:24

yii 验证码的使用和验证过程的相关文章

Yii 验证码验证

控制器如下 LoginForm如下 视图如下 <div class="loginbody">            <span class="systemlogo"></span>             <div class="loginbox">                <ul>                    <?php $form = $this->beg

yii 验证码那点事儿

今天要使用yii验证码, 不过, 这个验证码是整站通用的, 也就是说, 有个表单的提交是使用ajax方式来提交, 整站, 不管在哪个地方, 都能点出来此窗口, 来提交信息 关于yii验证码, framework/web/widgets/captcha/CCaptcha.php里, 它源码里有这么一部分 >---protected function renderImage() >---{ >--->---if(!isset($this->imageOptions['id']))

yii 验证码 CCaptcha的总结(转)

今天用到yii的验证码 ccaptcha,经过在网上搜寻 找到以下例子: 1.在controller中加入代码 (1)启用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <?php public function actions()     {         return array(             // 启用验证码组件             'captcha'=>array(                 'class'=>'CCaptchaA

安卓验证码的生成与验证

1.生成验证码 package com.medees.dazhima_android; import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint; import java.util.Random; /** * * Created by root on 14-7-10. * * 绘制注册界面验证码 * */publ

C#发送邮件异常:根据验证过程,远程证书无效

今天在做发送邮件功能时,开始用qq邮箱和163邮箱都可以正常发送,后再改用我公司的邮箱和smtp时竟然报错了. 异常提示-----"根据验证过程,远程证书无效",后来通过查询资料解决该问题,上代码: using log4net; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Mail; using System.Net.Secur

爬虫登陆验证过程

混合模式 结合二.三两大步,通过模拟点击快速拿到cookie,虽然效率低,但可以减少数据包分析的时间以及解决搞不定ajax登陆验证的烦恼,然后继续用urllib2拼接cookie继续快速获取数据.分下面两步: a. 从selenium中拿到cookie b. 添加cookie给urllib2使用 方法1:使用CookieJar,可参考<Creating Custom Cookies for HTTP Requests> 方法2:直接拼凑一个名称是"Cookie"的heade

yii表单的各种验证

/验证规则详细配置 public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('username', 'required' ,'message'=>'用户名必须填写'), // array('username', 'required' ,'requiredValue'=>'ab

架构验证过程发现非数据类型错误 validation found non-data type errors

问题: infopath报一下错误 validation found non-data type errors 架构验证过程发现非数据类型错误 原因: 重复表字段在后台代码里要一一对应,否则报错. 错误代码: //Remove 1st row. It's empty by default XPathNavigator FirstRowNode = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:e301/my:

yii 验证码的使用

在HappyController 中加入 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位 'maxLeng