Example of the Verification Code, logon, and exit functions implemented by the Yii framework.

Source: Internet
Author: User

Example of the Verification Code, logon, and exit functions implemented by the Yii framework.

This document describes the verification code, logon, and exit functions implemented by the Yii framework. We will share this with you for your reference. The details are as follows:

After a full afternoon, I finally got through and posted the code below.

Model

<?phpclass Auth extends CActiveRecord {  public static function model($className = __CLASS__) {    return parent::model($className);  }  public function tableName() {    return '{{auth}}';  }}

Note: My User table is auth, so the model is Auth. php

<? Phpclass IndexForm extends CFormModel {public $ a_account; public $ a_password; public $ rememberMe; public $ verifyCode; public $ _ identity; public function rules () {return array ('verifycode', 'captcha ', 'allowempty' =>! CCaptcha: checkRequirements (), 'message' => 'Enter the correct Verification Code '), array ('A _ account', 'required ', 'message' => 'username required '), array ('A _ password', 'required', 'message' => 'Password '), 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', 'user name or password does not exist ');}}} 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' => 'username', 'A _ password' => 'Password ', 'rememberme' => 'Remember logon status', 'verifycode' => 'verification code ');}}

Note: IndexForm can also be written as LoginForm, but it is already in the system and I have not replaced it. Also, note that the fields in your user table are usually password and username, while I have a_account and a_password.

Controller

<? Phpclass IndexController extends Controller {public function actions () {return array ('captcha '=> array ('class' => 'ccaptchaaction', 'width' => 100, 'height' => 50);} public function actionLogin () {if (Yii: app ()-> user-> id) {echo "<div> welcome ". yii: app ()-> user-> id. ", <a href = '". SITE_URL. "admin/index/logout'> exit </a> </div>";} else {$ model = new IndexForm (); if (isset ($ _ POST ['indexform']) {$ model-> attributes = $ _ POST ['indexform']; if ($ model-> validate () & $ model-> login () {echo "<div> welcome ". yii: app ()-> user-> id. ", <a href = '". SITE_URL. "admin/index/logout'> exit </a> </div>"; exit ;}$ this-> render ('login ', array ('model' => $ model) ;}} public function actionLogout () {Yii: app ()-> user-> logout (); $ this-> redirect (SITE_URL. 'admin/index/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('CActiveForm', array(  'id'            => 'login-form',  'enableClientValidation'  => true,  'clientOptions'       => array(    'validateOnSubmit'   => true  )));?>  <div class="row">    <?php echo $form->labelEx($model,'a_account'); ?>    <?php echo $form->textField($model,'a_account'); ?>    <?php echo $form->error($model,'a_account'); ?>  </div>  <div class="row">    <?php echo $form->labelEx($model,'a_password'); ?>    <?php echo $form->passwordField($model,'a_password'); ?>    <?php echo $form->error($model,'a_password'); ?>  </div>  <?php if(CCaptcha::checkRequirements()) { ?>  <div class="row">    <?php echo $form->labelEx($model, 'verifyCode'); ?>    <?php $this->widget('CCaptcha'); ?>    <?php echo $form->textField($model, 'verifyCode'); ?>    <?php echo $form->error($model, 'verifyCode'); ?>  </div>  <?php } ?>  <div class="row rememberMe">    <?php echo $form->checkBox($model,'rememberMe'); ?>    <?php echo $form->label($model,'rememberMe'); ?>    <?php echo $form->error($model,'rememberMe'); ?>  </div>  <div class="row buttons">    <?php echo CHtml::submitButton('Submit'); ?>  </div><?php $this->endWidget(); ?>

Modify UserIdentity. php under protected/components

<?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;    }  }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.