查看: 1078|回复: 0

[PHP学习] Yii框架实现的验证码、登录及退出功能示例

发表于 2018-3-31 08:00:03

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

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

Model

  1. <?php
  2. class Auth extends CActiveRecord {
  3. public static function model($className = __CLASS__) {
  4. return parent::model($className);
  5. }
  6. public function tableName() {
  7. return '{{auth}}';
  8. }
  9. }
复制代码

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

  1. <?php
  2. class IndexForm extends CFormModel {
  3. public $a_account;
  4. public $a_password;
  5. public $rememberMe;
  6. public $verifyCode;
  7. public $_identity;
  8. public function rules() {
  9. return array(
  10. array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(), 'message'=>'请输入正确的验证码'),
  11. array('a_account', 'required', 'message' => '用户名必填'),
  12. array('a_password', 'required', 'message' => '密码必填'),
  13. array('a_password', 'authenticate'),
  14. array('rememberMe', 'boolean'),
  15. );
  16. }
  17. public function authenticate($attribute, $params) {
  18. if (!$this->hasErrors()) {
  19. $this->_identity = new UserIdentity($this->a_account, $this->a_password);
  20. if (!$this->_identity->authenticate()) {
  21. $this->addError('a_password', '用户名或密码不存在');
  22. }
  23. }
  24. }
  25. public function login() {
  26. if ($this->_identity === null) {
  27. $this->_identity = new UserIdentity($this->a_account, $this->a_password);
  28. $this->_identity->authenticate();
  29. }
  30. if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
  31. $duration = $this->rememberMe ? 60*60*24*7 : 0;
  32. Yii::app()->user->login($this->_identity, $duration);
  33. return true;
  34. } else {
  35. return false;
  36. }
  37. }
  38. public function attributeLabels() {
  39. return array(
  40. 'a_account' => '用户名',
  41. 'a_password' => '密码',
  42. 'rememberMe' => '记住登录状态',
  43. 'verifyCode' => '验证码'
  44. );
  45. }
  46. }
复制代码

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

Controller

  1. <?php
  2. class IndexController extends Controller {
  3. public function actions() {
  4. return array(
  5. 'captcha' => array(
  6. 'class' => 'CCaptchaAction',
  7. 'width'=>100,
  8. 'height'=>50
  9. )
  10. );
  11. }
  12. public function actionLogin() {
  13. if (Yii::app()->user->id) {
  14. echo "<div>欢迎" . Yii::app()->user->id . ",<a href='" . SITE_URL . "admin/index/logout'>退出</a></div>";
  15. } else {
  16. $model = new IndexForm();
  17. if (isset($_POST['IndexForm'])) {
  18. $model->attributes = $_POST['IndexForm'];
  19. if ($model->validate() && $model->login()) {
  20. echo "<div>欢迎" . Yii::app()->user->id . ",<a href='" . SITE_URL . "admin/index/logout'>退出</a></div>";exit;
  21. }
  22. }
  23. $this->render('login', array('model' => $model));
  24. }
  25. }
  26. public function actionLogout() {
  27. Yii::app()->user->logout();
  28. $this->redirect(SITE_URL . 'admin/index/login');
  29. }
  30. }
复制代码

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

view

  1. <meta http-equiv="content-type" content="text/html;charset=utf-8">
  2. <?php
  3. $form = $this->beginWidget('CActiveForm', array(
  4. 'id' => 'login-form',
  5. 'enableClientValidation' => true,
  6. 'clientOptions' => array(
  7. 'validateOnSubmit' => true
  8. )
  9. ));
  10. ?>
  11. <div class="row">
  12. <?php echo $form->labelEx($model,'a_account'); ?>
  13. <?php echo $form->textField($model,'a_account'); ?>
  14. <?php echo $form->error($model,'a_account'); ?>
  15. </div>
  16. <div class="row">
  17. <?php echo $form->labelEx($model,'a_password'); ?>
  18. <?php echo $form->passwordField($model,'a_password'); ?>
  19. <?php echo $form->error($model,'a_password'); ?>
  20. </div>
  21. <?php if(CCaptcha::checkRequirements()) { ?>
  22. <div class="row">
  23. <?php echo $form->labelEx($model, 'verifyCode'); ?>
  24. <?php $this->widget('CCaptcha'); ?>
  25. <?php echo $form->textField($model, 'verifyCode'); ?>
  26. <?php echo $form->error($model, 'verifyCode'); ?>
  27. </div>
  28. <?php } ?>
  29. <div class="row rememberMe">
  30. <?php echo $form->checkBox($model,'rememberMe'); ?>
  31. <?php echo $form->label($model,'rememberMe'); ?>
  32. <?php echo $form->error($model,'rememberMe'); ?>
  33. </div>
  34. <div class="row buttons">
  35. <?php echo CHtml::submitButton('Submit'); ?>
  36. </div>
  37. <?php $this->endWidget(); ?>
复制代码

同时修改项目下protected/components下的UserIdentity.php

  1. <?php
  2. /**
  3. * UserIdentity represents the data needed to identity a user.
  4. * It contains the authentication method that checks if the provided
  5. * data can identity the user.
  6. */
  7. class UserIdentity extends CUserIdentity
  8. {
  9. /**
  10. * Authenticates a user.
  11. * The example implementation makes sure if the username and password
  12. * are both 'demo'.
  13. * In practical applications, this should be changed to authenticate
  14. * against some persistent user identity storage (e.g. database).
  15. * @return boolean whether authentication succeeds.
  16. */
  17. public function authenticate()
  18. {
  19. /*
  20. $users=array(
  21. // username => password
  22. 'demo'=>'demo',
  23. 'admin'=>'admin',
  24. );
  25. if(!isset($users[$this->username]))
  26. $this->errorCode=self::ERROR_USERNAME_INVALID;
  27. elseif($users[$this->username]!==$this->password)
  28. $this->errorCode=self::ERROR_PASSWORD_INVALID;
  29. else
  30. $this->errorCode=self::ERROR_NONE;
  31. return !$this->errorCode;
  32. */
  33. $user_model = Auth::model()->find('a_account=:name',array(':name'=>$this->username));
  34. if($user_model === null){
  35. $this -> errorCode = self::ERROR_USERNAME_INVALID;
  36. return false;
  37. } else if ($user_model->a_password !== md5($this -> password)){
  38. $this->errorCode=self::ERROR_PASSWORD_INVALID;
  39. return false;
  40. } else {
  41. $this->errorCode=self::ERROR_NONE;
  42. return true;
  43. }
  44. }
  45. }
复制代码

更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。



回复

使用道具 举报