国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

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

2024-05-04 23:57:22
字體:
來源:轉載
供稿:網友

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

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

Model

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

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

<?phpclass 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

<?phpclass 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(); ?>

同時修改項目下protected/components下的UserIdentity.php

<?php/** * UserIdentity represents the data needed to identity a user. * It contains the authentication method that checks if the provided * data can identity the user. */class UserIdentity extends CUserIdentity{  /**   * Authenticates a user.   * The example implementation makes sure if the username and password   * are both 'demo'.   * In practical applications, this should be changed to authenticate   * against some persistent user identity storage (e.g. database).   * @return boolean whether authentication succeeds.   */  public function authenticate()  {    /*    $users=array(      // username => password      'demo'=>'demo',      'admin'=>'admin',    );    if(!isset($users[$this->username]))      $this->errorCode=self::ERROR_USERNAME_INVALID;    elseif($users[$this->username]!==$this->password)      $this->errorCode=self::ERROR_PASSWORD_INVALID;    else      $this->errorCode=self::ERROR_NONE;    return !$this->errorCode;    */    $user_model = Auth::model()->find('a_account=:name',array(':name'=>$this->username));    if($user_model === null){      $this -> errorCode = self::ERROR_USERNAME_INVALID;      return false;    } else if ($user_model->a_password !== md5($this -> password)){      $this->errorCode=self::ERROR_PASSWORD_INVALID;      return false;    } else {      $this->errorCode=self::ERROR_NONE;      return true;    }  }}

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


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 宝兴县| 岳西县| 平度市| 阿荣旗| 抚宁县| 长海县| 泉州市| 桂东县| 彭州市| 唐海县| 英超| 邯郸市| 盐边县| 清流县| 鹿泉市| 益阳市| 石河子市| 五家渠市| 晋城| 永吉县| 恩施市| 崇左市| 彰化县| 牙克石市| 尚义县| 宁国市| 彭泽县| 平陆县| 遵义市| 衡山县| 祁门县| 三穗县| 铅山县| 尚义县| 文成县| 梧州市| 新巴尔虎左旗| 鲁山县| 永寿县| 科尔| 大埔县|