关于Yii 验证码(captcha)的一些…

来源:互联网 发布:淘宝手机端优惠券链接 编辑:程序博客网 时间:2024/05/24 07:32

关于Yii 验证码(captcha)的一些资料

关于如果在表单中添加验证码(captcha),官方文档中一直没有的详细的介绍。下面就如何在YIIblog的评论表单中添加验证码进行说明:
将以下代码插入评论的视图文件: views/comment/_form.php

1.  <?php if(extension_loaded('gd')): ?>

2.        <divclass="row">

3.          <?php echoCHtml::activeLabelEx($model, 'verifyCode') ?>

4.        <div>

5.        <?php$this->widget('CCaptcha'); ?>

6.        <?phpecho CHtml::activeTextField($model,'verifyCode');?>

7.        </div>

8.        <divclass="hint">Please enter the letters as they areshown in the image above.

9.        <br/>Lettersare not case-sensitive.</div>

10.       </div>

11.    <?php endif; ?>


在模型Comment.php中添加如下代码:

1.  public $verifyCode;


在rules()下添加

1.  array('verifyCode', 'captcha','allowEmpty'=>!Yii::app()->user->isGuest),


在attributeLabels()下添加:

1.  'verifyCode' =>'Verification Code',


在控制器文件controllers/PostController.php 加添加如下动作:

1.  public functionactions()    {

2.        returnarray(

3.          'captcha'=>array(

4.             'class'=>'CCaptchaAction',

5.             'backColor'=>0xFFFFFF,

6.           ),

7.        );

8.     }


此时, 在 rules中,在 'deny all'前,增加如下代码:

1.  array('allow',

2.        'actions'=>array('captcha'),

3.        'users'=>array('*'),

4.  ),


此时,可显示验证码。

 

 

通过使用小物件,提高验证码的通用性
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CForm为Yii提供了表单自动生成功能,非常灵活和强大。通过传入数组和Model,就可以自动生成表单。有些人不喜欢硬编码,认为这样失去了灵活性。但是Yii中几乎所有的CSS属性都可以自定义,而我一般使用默认的CSS属性值,这样带来的好处是可以在协作的时候统一CSS属性名。

但是,验证码(Captcha)不是一个CInputWidget,所以不能使用简单的type=>’CCapthca’来将一个字段来显示为验证码。现有的解决方案是给CForm添加一个字符串元素,但是比较麻烦,需要手工写一些HTML。但是,写一个简单的Widget就可以了啊。为什么要搞得那么复杂。

比如这样:

1.  classCaptchaWidget extends CInputWidget

2.  {

3.  public $hint =null;

4.   

5.  public functioninit()

6.  {

7.  if(!extension_loaded(‘gd’))

8.  throw newCException(‘CaptchaWidget needGD’);

9.  }

10.  

11. public functionrun()

12. {

13.echoCHtml::activeTextField($this->model,’captcha’,$this->htmlOptions);

14.Yii::app()->controller->widget(‘CCaptcha’);

15. $this->hint===null? ” :‘<div>’.$this->hint.’</div>’;

16. }

17. }


使用的时候在表单配置数组里面添加一行‘captcha’=>array(‘type’=>’CaptchaWidge’)。当然,前提是你的Model里面需要有一个Captcha属性。对于我来说,因为我是代码洁癖,所以对于需要验证码的情况我都不使用原来的Model(比如User的Model),而是使用CFormModel来新建一个专用的Model。




------------------------------------------------------------------------------------华丽丽的分割线-----------------------------------------------------------------------------------------

以上为之前查阅的别人的东西,不记得出处在哪里了,很抱歉.以后找到了,一定注明.(现在只是觉得以上这个文章写的比较全.)

下面说说我自己的做法:

总共会修改以下3个文件:model中文件/controller中/view中

1.在model层的文件中插入

public $verifyCode;

在rules()中插入

array('verifyCode', 'captcha','allowEmpty'=>!CCaptcha::checkRequirements()),

            在attributeLabels()中插入

        'verifyCode'=>'验证码';


在rules()还要添加

1.  array('allow',

2.       'actions'=>array('captcha'),

3.       'users'=>array('*'),

4.  ),


2.public function actions()
{
       return array(
       // captcha action renders the CAPTCHA imagedisplayed on the contact page
             'captcha'=>array(
             'class'=>'CCaptchaAction',
             'backColor'=>0xFFFFFF, //背景颜色
             'minLength'=>4,  //最短为4位
             'maxLength'=>4,   //是长为4位
             'transparent'=>true,  //显示为透明
       ),
       );
}

//这一小段摘自http://www.yiichina.org/forum/thread-22-1-1.html

3. 最后在View层的文件中插入

<?php if(CCaptcha::checkRequirements()):?>
          <div>
              <?php$this->widget('CCaptcha'); ?>
              <?php echo$form->textField($model,'verifyCode');?>
          </div>
      <?php endif;?>

原文:http://blog.csdn.net/zhaoshl_368/article/details/6833883