最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Yii使用Captcha驗證碼的方法

 更新時間:2015年12月28日 10:38:35   作者:yuhui_fish  
這篇文章主要介紹了Yii使用Captcha驗證碼的方法,結(jié)合實例形式分析了Yii使用Captcha驗證碼的MVC三層具體實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了Yii使用Captcha驗證碼的方法。分享給大家供大家參考,具體如下:

詳細代碼可參考:yii自帶的示例代碼post項目,里面有一個contact表單用到了驗證碼.

1. Model:

將驗證碼加入UserLogin的一個屬性:

class UserLogin extends CFormModel
{
 public $username;
 public $password;
 public $rememberMe;
 public $verifyCode;
 public function rules()
 {
  return array(
   // username and password are required
   array('username, password,verifyCode', 'required'),
   // rememberMe needs to be a boolean
   array('rememberMe', 'boolean'),
   // password needs to be authenticated
   array('password', 'authenticate'),
   // verifyCode needs to be entered correctly
   array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
  );
 }
 /**
  * Declares attribute labels.
  */
 public function attributeLabels()
 {
  return array(
   'rememberMe'=>Yii::t('user',"Remember me next time"),
   'username'=>Yii::t('user',"username or email"),
   'password'=>Yii::t('user',"password"),
   'verifyCode'=>Yii::t('user','Verification Code'),
  );
 }
}

2. Controller

在LoginController控制器加入映射動作CCaptchaAction

public function actions()
{
 return array(
  // captcha action renders the CAPTCHA image displayed on the contact page
  'captcha'=>array(
   'class'=>'CCaptchaAction',
   'backColor'=>0xf4f4f4,
   'padding'=>0,
   'height'=>30,
   'maxLength'=>4,
  ),
  );
}
ublic function actionLogin()
{
 if (Yii::app()->user->isGuest) {
  $model=new UserLogin;
  // collect user input data
  if(isset($_POST['UserLogin']))
  {
   $model->attributes=$_POST['UserLogin'];
//在此核對驗證碼
   if($this->createAction('captcha')->validate($model->verifyCode, false))
   {
    // validate user input and redirect to previous page if valid
    if($model->validate()) {
    //admin login only
    if( Yii::app()->getModule('user')->isAdmin()==1 )
    {
    $this->lastViset();
    if (strpos(Yii::app()->user->returnUrl,'/index.php')!==false)
     $this->redirect(Yii::app()->controller->module->returnUrl);
    else
     $this->redirect(Yii::app()->user->returnUrl);
    }else
    {//if no admin when login out
     $this->redirect(Yii::app()->controller->module->logoutUrl);
    }
   }
   }else
   {//提示錯誤
    $model->addError('verifyCode','驗證碼不對');
   }
  }
  // display the login form
  $this->render('/user/login',array('model'=>$model));
 } else
  $this->redirect(Yii::app()->controller->module->returnUrl);
}

在驗證用戶名密碼前,檢查驗證碼:

if($this->createAction('captcha')->validate($model->verifyCode, false))
{

3. view

在視圖中顯示驗證碼圖片,輸入框

<?php $this->widget('CCaptcha'); ?>
  <?php echo CHtml::activeTextField($model,'verifyCode',array('tabindex'=>1)); ?>
<img src="http://www.XXXX.net/uploads/123456.jpg" alt="">

希望本文所述對大家基于Yii框架的PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論

泾川县| 临湘市| 长宁区| 丹江口市| 张北县| 北辰区| 甘德县| 玛沁县| 涪陵区| 忻城县| 浦江县| 鄂伦春自治旗| 邢台市| 水城县| 石台县| 营口市| 靖边县| 韩城市| 城口县| 滁州市| 闽侯县| 土默特左旗| 鄂托克前旗| 鹤庆县| 奉化市| 区。| 大田县| 定结县| 通州市| 汉沽区| 奉贤区| 临潭县| 莲花县| 常德市| 晋宁县| 芜湖市| 建水县| 浑源县| 南召县| 康马县| 芒康县|