• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

有关Yii框架实现验证码、登录及退出功能实例讲解

php 搞代码 4年前 (2022-01-22) 11次浏览 已收录 0个评论

这篇文章主要介绍了Yii框架实现的验证码、登录及退出功能,结合具体实例形式分析了基于Yii框架实现的用户验证登录及退出操作相关步骤与操作技巧,需要的朋友可以参考下

本文实例讲述了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(   <b>6本文来源gao@dai!ma.com搞$代^码!网7</b><pre>搞gaodaima代码

'captcha' => array( 'class' => 'CCaptchaAction', 'width'=>100, 'height'=>50 ) ); } public function actionLogin() { if (Yii::app()->user->id) { echo "<p>欢迎" . Yii::app()->user->id . ",<a href='" . SITE_URL . "admin/index/logout'>退出</a></p>"; } else { $model = new IndexForm(); if (isset($_POST['IndexForm'])) { $model->attributes = $_POST['IndexForm']; if ($model->validate() && $model->login()) { echo "<p>欢迎" . Yii::app()->user->id . ",<a href='" . SITE_URL . "admin/index/logout'>退出</a></p>";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  )));?>  <p class="row">    <?php echo $form->labelEx($model,'a_account'); ?>    <?php echo $form->textField($model,'a_account'); ?>    <?php echo $form->error($model,'a_account'); ?>  </p>  <p class="row">    <?php echo $form->labelEx($model,'a_password'); ?>    <?php echo $form->passwordField($model,'a_password'); ?>    <?php echo $form->error($model,'a_password'); ?>  </p>  <?php if(CCaptcha::checkRequirements()) { ?>  <p class="row">    <?php echo $form->labelEx($model, 'verifyCode'); ?>    <?php $this->widget('CCaptcha'); ?>    <?php echo $form->textField($model, 'verifyCode'); ?>    <?php echo $form->error($model, 'verifyCode'); ?>  </p>  <?php } ?>  <p class="row rememberMe">    <?php echo $form->checkBox($model,'rememberMe'); ?>    <?php echo $form->label($model,'rememberMe'); ?>    <?php echo $form->error($model,'rememberMe'); ?>  </p>  <p class="row buttons">    <?php echo CHtml::submitButton('Submit'); ?>  </p><?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框架实现验证码、登录及退出功能实例讲解的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:有关Yii框架实现验证码、登录及退出功能实例讲解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址