找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1841|回复: 0
打印 上一主题 下一主题

Yii框架实现的验证码、登录及退出功能示例

[复制链接]

2560

主题

2560

帖子

7622

积分

论坛元老

Rank: 8Rank: 8

积分
7622
跳转到指定楼层
楼主
发表于 2018-2-14 05:25:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            本文实例讲述了Yii框架实现的验证码、登录及退出功能。分享给大家供大家参考,具体如下:
捣鼓了一下午,总算走通了,下面贴出代码。
Model
注:我的用户表是auth,所以模型是Auth.php
!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
array(
        'class' => 'CCaptchaAction',
        'width'=>100,
        'height'=>50
      )
    );
  }
  public function actionLogin() {
    if (Yii::app()->user->id) {
      echo "欢迎" . Yii::app()->user->id . ",退出";
    } else {
      $model = new IndexForm();
      if (isset($_POST['IndexForm'])) {
        $model->attributes = $_POST['IndexForm'];
        if ($model->validate() && $model->login()) {
          echo "欢迎" . Yii::app()->user->id . ",退出";exit;
        }
      }
      $this->render('login', array('model' => $model));
    }
  }
  public function actionLogout() {
    Yii::app()->user->logout();
    $this->redirect(SITE_URL . 'admin/index/login');
  }
}
注:第一个方法是添加验证码的
view
beginWidget('CActiveForm', array(
  'id'            => 'login-form',
  'enableClientValidation'  => true,
  'clientOptions'       => array(
    'validateOnSubmit'   => true
  )
));
?>
  
    labelEx($model,'a_account'); ?>
    textField($model,'a_account'); ?>
    error($model,'a_account'); ?>
  
  
    labelEx($model,'a_password'); ?>
    passwordField($model,'a_password'); ?>
    error($model,'a_password'); ?>
  
  
  
    labelEx($model, 'verifyCode'); ?>
    widget('CCaptcha'); ?>
    textField($model, 'verifyCode'); ?>
    error($model, 'verifyCode'); ?>
  
  
  
    checkBox($model,'rememberMe'); ?>
    label($model,'rememberMe'); ?>
    error($model,'rememberMe'); ?>
  
  
   
  
endWidget(); ?>
同时修改项目下protected/components下的UserIdentity.php
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相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
            
            
您可能感兴趣的文章:
  • yii实现创建验证码实例解析
  • Yii使用Captcha验证码的方法
  • yii2中添加验证码的实现方法
  • Yii2增加验证码步骤详解
  • Yii2简单实现给表单添加验证码的方法
  • Yii2下点击验证码的切换实例代码
  • Yii输入正确验证码却验证失败的解决方法
  • Yii2 如何在modules中添加验证码的方法
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端