yii登陆中添加验证码

1、在SiteController中添加如下代码:

Php代码  收藏代码
  1. /** 
  2.  * Declares class-based actions. 
  3.  */  
  4. public function actions() {  
  5.     return array(  
  6.         // captcha action renders the CAPTCHA image displayed on the contact page  
  7.         'captcha' => array(  
  8.             'class' => 'CCaptchaAction',  
  9.             'backColor' => 0xFFFFFF,  
  10.         ),  
  11.         // page action renders "static" pages stored under 'protected/views/site/pages'  
  12.         // They can be accessed via: index.php?r=site/page&view=FileName  
  13.         'page' => array(  
  14.             'class' => 'CViewAction',  
  15.         ),  
  16.     );  
  17. }  

2、在 Model/LoginForm.php中

           添加一个属性: public $verifyCode;

           在rules数组最后添加:array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),

3、在视图中,在view/login.php中需要添加验证码的地方写上:

<input id='LoginForm_verifyCode' type="text" name="LoginForm[verifyCode]" >

  

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

// 下面这个可以点击图片进行换验证码

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

原文地址:https://www.cnblogs.com/liuwenbohhh/p/4349439.html