久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Yii框架實現的驗證碼、登錄及退出功能示例

這篇文章主要介紹了Yii框架實現的驗證碼、登錄及退出功能,結合具體實例形式分析了基于Yii框架實現的用戶驗證登錄及退出操作相關步驟與操作技巧,需要的朋友可以參考下

本文實例講述了Yii框架實現的驗證碼、登錄及退出功能。分享給大家供大家參考,具體如下:

搗鼓了一下午,總算走通了,下面貼出代碼。

Model

<?php
class Auth extends CActiveRecord {
  public static function model($className = __CLASS__) {
    return parent::model($className);
  }
  public function tableName() {
    return '{{auth}}';
  }
}

注:我的用戶表是auth,所以模型是Auth.php

<?php
class IndexForm extends CFormModel {
  public $a_account;
  public $a_password;
  public $rememberMe;
  public $verifyCode;
  public $_identity;
  public function rules() {
    return array(
      array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(), 'message'=>'請輸入正確的驗證碼'),
      array('a_account', 'required', 'message' => '用戶名必填'),
      array('a_password', 'required', 'message' => '密碼必填'),
      array('a_password', 'authenticate'),
      array('rememberMe', 'boolean'),
    );
  }
  public function authenticate($attribute, $params) {
    if (!$this->hasErrors()) {
      $this->_identity = new UserIdentity($this->a_account, $this->a_password);
      if (!$this->_identity->authenticate()) {
        $this->addError('a_password', '用戶名或密碼不存在');
      }
    }
  }
  public function login() {
    if ($this->_identity === null) {
      $this->_identity = new UserIdentity($this->a_account, $this->a_password);
      $this->_identity->authenticate();
    }
    if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
      $duration = $this->rememberMe ? 60*60*24*7 : 0;
      Yii::app()->user->login($this->_identity, $duration);
      return true;
    } else {
      return false;
    }
  }
  public function attributeLabels() {
    return array(
      'a_account'   => '用戶名',
      'a_password'   => '密碼',
      'rememberMe'  => '記住登錄狀態',
      'verifyCode'  => '驗證碼'
    );
  }
}

注:IndexForm也可以寫成LoginForm,只是系統內已經有了,我就沒有替換它,同時注意看自己用戶表的字段,一般是password和username,而我的是a_account和a_password

Controller

<?php
class IndexController extends Controller {
  public function actions() {
    return array(
      'captcha' => array(
        'class' => 'CCaptchaAction',
        'width'=>100,
        'height'=>50
      )
    );
  }
  public function actionLogin() {
    if (Yii::app()->user->id) {
      echo "<div>歡迎" . Yii::app()->user->id . ",<a href='" . SITE_URL . "admin/index/logout'>退出</a></div>";
    } else {
      $model = new IndexForm();
      if (isset($_POST['IndexForm'])) {
        $model->attributes = $_POST['IndexForm'];
        if ($model->validate() && $model->login()) {
          echo "<div>歡迎" . Yii::app()->user->id . ",<a href='" . SITE_URL . "admin/index/logout'>退出</a></div>";exit;
        }
      }
      $this->render('login', array('model' => $model));
    }
  }
  public function actionLogout() {
    Yii::app()->user->logout();
    $this->redirect(SITE_URL . 'admin/index/login');
  }
}

注:第一個方法是添加驗證碼的

view

<meta http-equiv="content-type" content="text/html;charset=utf-8">
<?php
$form = $this->beginWidget('CActiveForm', array(
  'id'            => 'login-form',
  'enableClientValidation'  => true,
  'clientOptions'       => array(
    'validateOnSubmit'   => true
  )
));
?>
  <div class="row">
    <?php echo $form->labelEx($model,'a_account'); ?>
    <?php echo $form->textField($model,'a_account'); ?>
    <?php echo $form->error($model,'a_account'); ?>
  </div>
  <div class="row">
    <?php echo $form->labelEx($model,'a_password'); ?>
    <?php echo $form->passwordField($model,'a_password'); ?>
    <?php echo $form->error($model,'a_password'); ?>
  </div>
  <?php if(CCaptcha::checkRequirements()) { ?>
  <div class="row">
    <?php echo $form->labelEx($model, 'verifyCode'); ?>
    <?php $this->widget('CCaptcha'); ?>
    <?php echo $form->textField($model, 'verifyCode'); ?>
    <?php echo $form->error($model, 'verifyCode'); ?>
  </div>
  <?php } ?>
  <div class="row rememberMe">
    <?php echo $form->checkBox($model,'rememberMe'); ?>
    <?php echo $form->label($model,'rememberMe'); ?>
    <?php echo $form->error($model,'rememberMe'); ?>
  </div>
  <div class="row buttons">
    <?php echo CHtml::submitButton('Submit'); ?>
  </div>
<?php $this->endWidget(); ?>

【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。

相關文檔推薦

這篇文章主要為大家詳細介紹了Laravel下生成驗證碼的類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
這篇文章主要為大家詳細介紹了PHP實現驗證碼校驗功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
這篇文章主要介紹了php利用云片網實現短信驗證碼功能的示例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
這篇文章主要介紹了PHP驗證碼類文件及調用方式代碼詳解,需要的朋友可以參考下
下面小編就為大家帶來一篇修改yii2.0用戶登錄使用的user表為其它的表實現方法(推薦)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
這篇文章主要為大家詳細介紹了一個實用的php驗證碼類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
主站蜘蛛池模板: 国产成人免费网站 | 99久久中文字幕三级久久日本 | 精品国产乱码久久久久久蜜柚 | 国产一区日韩在线 | 91精品国产综合久久久久 | 国产欧美在线观看 | 一级大片| 日韩免费三级 | 97国产一区二区 | 久久国产精品久久久久久久久久 | 国产午夜视频 | av中文字幕在线观看 | 精品啪啪| 久久久国产一区二区三区 | 久久久久久国产 | 中文字幕亚洲专区 | 看真人视频一级毛片 | 一本色道精品久久一区二区三区 | 亚洲一区二区 | 91精品国产乱码久久久久久久久 | 视频精品一区 | 国产精品1区2区3区 国产在线观看一区 | 国产午夜精品久久久 | 亚洲欧美综合精品另类天天更新 | 国产一区欧美一区 | 精品在线观看一区二区 | 黄视频免费观看 | 日韩av福利在线观看 | 亚洲精品区 | 一级中国毛片 | 亚洲www.| 成人在线视频一区二区三区 | 亚洲精品免费观看 | 久久久免费少妇高潮毛片 | 色综合天天天天做夜夜夜夜做 | 欧美黄色片在线观看 | 免费簧片视频 | 久久成人久久 | 在线观看中文字幕视频 | 国产精品一区二区免费看 | 日本三级做a全过程在线观看 |