久久久久久久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驗證碼類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
主站蜘蛛池模板: 国产成人综合在线 | 亚洲精品免费在线 | 成人自拍视频 | 国产在线一区观看 | 亚洲午夜精品一区二区三区他趣 | 蜜桃视频在线观看免费视频网站www | 91porn成人精品 | 欧美另类视频 | 午夜一级做a爰片久久毛片 精品综合 | 午夜色婷婷 | 欧美视频一级 | 欧美日韩在线一区二区 | 91精品亚洲 | 色综久久 | 亚洲国产一区二区三区 | 中文字幕高清免费日韩视频在线 | 中文视频在线 | 九九热精品免费 | 久草网视频| 日韩在线h | 午夜影院视频 | 天堂一区二区三区四区 | 国产中文字幕在线 | 自拍第一页 | 中文字幕不卡一区 | 欧美日韩久久久 | 黄色免费观看 | 亚洲69p| 北条麻妃一区二区三区在线视频 | 国产中文在线 | 精品国产乱码久久久久久中文 | 欧美三区在线观看 | 欧美一区视频 | 国产精品久久久久久久午夜片 | 在线日韩欧美 | 四虎影院一区二区 | 国产999精品久久久久久 | 日韩欧美天堂 | 亚洲一区二区三区国产 | www国产成人免费观看视频,深夜成人网 | 18av在线播放 |