登录  /  注册

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

巴扎黑
发布: 2017-08-13 14:34:36
原创
1111人浏览过

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

本文实例讲述了Yii框架实现的验证码、登录及退出功能。分享给大家供大家参考,具体如下:

捣鼓了一下午,总算走通了,下面贴出代码。

Model


<?php
class Auth extends CActiveRecord {
  public static function model($className = __CLASS__) {
    return parent::model($className);
  }
  public function tableName() {
    return &#39;{{auth}}&#39;;
  }
}
登录后复制

注:我的用户表是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(&#39;verifyCode&#39;, &#39;captcha&#39;, &#39;allowEmpty&#39; => !CCaptcha::checkRequirements(), &#39;message&#39;=>&#39;请输入正确的验证码&#39;),
      array(&#39;a_account&#39;, &#39;required&#39;, &#39;message&#39; => &#39;用户名必填&#39;),
      array(&#39;a_password&#39;, &#39;required&#39;, &#39;message&#39; => &#39;密码必填&#39;),
      array(&#39;a_password&#39;, &#39;authenticate&#39;),
      array(&#39;rememberMe&#39;, &#39;boolean&#39;),
    );
  }
  public function authenticate($attribute, $params) {
    if (!$this->hasErrors()) {
      $this->_identity = new UserIdentity($this->a_account, $this->a_password);
      if (!$this->_identity->authenticate()) {
        $this->addError(&#39;a_password&#39;, &#39;用户名或密码不存在&#39;);
      }
    }
  }
  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(
      &#39;a_account&#39;   => &#39;用户名&#39;,
      &#39;a_password&#39;   => &#39;密码&#39;,
      &#39;rememberMe&#39;  => &#39;记住登录状态&#39;,
      &#39;verifyCode&#39;  => &#39;验证码&#39;
    );
  }
}
登录后复制

注:IndexForm也可以写成LoginForm,只是系统内已经有了,我就没有替换它,同时注意看自己用户表的字段,一般是password和username,而我的是a_account和a_password

Controller


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

注:第一个方法是添加验证码的

view


<meta http-equiv="content-type" content="text/html;charset=utf-8">
<?php
$form = $this->beginWidget(&#39;CActiveForm&#39;, array(
  &#39;id&#39;            => &#39;login-form&#39;,
  &#39;enableClientValidation&#39;  => true,
  &#39;clientOptions&#39;       => array(
    &#39;validateOnSubmit&#39;   => true
  )
));
?>
  <p class="row">
    <?php echo $form->labelEx($model,&#39;a_account&#39;); ?>
    <?php echo $form->textField($model,&#39;a_account&#39;); ?>
    <?php echo $form->error($model,&#39;a_account&#39;); ?>
  </p>
  <p class="row">
    <?php echo $form->labelEx($model,&#39;a_password&#39;); ?>
    <?php echo $form->passwordField($model,&#39;a_password&#39;); ?>
    <?php echo $form->error($model,&#39;a_password&#39;); ?>
  </p>
  <?php if(CCaptcha::checkRequirements()) { ?>
  <p class="row">
    <?php echo $form->labelEx($model, &#39;verifyCode&#39;); ?>
    <?php $this->widget(&#39;CCaptcha&#39;); ?>
    <?php echo $form->textField($model, &#39;verifyCode&#39;); ?>
    <?php echo $form->error($model, &#39;verifyCode&#39;); ?>
  </p>
  <?php } ?>
  <p class="row rememberMe">
    <?php echo $form->checkBox($model,&#39;rememberMe&#39;); ?>
    <?php echo $form->label($model,&#39;rememberMe&#39;); ?>
    <?php echo $form->error($model,&#39;rememberMe&#39;); ?>
  </p>
  <p class="row buttons">
    <?php echo CHtml::submitButton(&#39;Submit&#39;); ?>
  </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 &#39;demo&#39;.
   * 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
      &#39;demo&#39;=>&#39;demo&#39;,
      &#39;admin&#39;=>&#39;admin&#39;,
    );
    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(&#39;a_account=:name&#39;,array(&#39;:name&#39;=>$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中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号