Examples of implementation of verification code, login and exit functions in Yii framework

巴扎黑
Release: 2023-03-15 06:48:01
Original
1315 people have browsed it

This article mainly introduces the verification code, login and logout functions implemented by the Yii framework, and analyzes the relevant steps and operating techniques for user verification login and logout operations based on the Yii framework based on specific examples. Friends in need can refer to the following

The examples in this article describe the verification code, login and logout functions implemented by the Yii framework. I share it with you for your reference. The details are as follows:

After tinkering for an afternoon, I finally got it working. The code is posted below.

Model


<?php
class Auth extends CActiveRecord {
  public static function model($className = __CLASS__) {
    return parent::model($className);
  }
  public function tableName() {
    return &#39;{{auth}}&#39;;
  }
}
Copy after login

Note: My user table is auth, so the model is 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;
    );
  }
}
Copy after login

Note: IndexForm can also be written as LoginForm, but it already exists in the system, so I did not replace it. At the same time, pay attention to the fields of your user table, usually password and username, and mine are a_account and 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;);
  }
}
Copy after login

Note: The first method is to add the verification code

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(); ?>
Copy after login

Also modify UserIdentity.php under protected/components under the project


##

<?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;
    }
  }
}
Copy after login

The above is the detailed content of Examples of implementation of verification code, login and exit functions in Yii framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template