About the YII framework Implementation verification code, login and Exit Function Example explained

Source: Internet
Author: User
This article mainly introduces the verification code, login and Exit function implemented by YII framework, and analyzes the steps and operation skills of user authentication login and exit operation based on YII framework, and the necessary friends can refer to it according to the specific instance form.

This paper describes the verification code, login and Exit function of Yii framework. Share to everyone for your reference, as follows:

The afternoon, finally walked through, the following posted code.

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 ' = ' Please enter the correct verification code '), Array (' A_account ', ' Required ', ' message ' = ' username required '), Array (' A_password ', ' Required ', ' message ' = ' password required '), Array (' A_password ', ' Authenticate '), Array (' Remembe  RMe ', ' Boolean '),); Public function Authenticate ($attribute, $params) {if (! $this->haserrors ()) {$this->_identity = new Use      Ridentity ($this->a_account, $this->a_password);      if (! $this->_identity->authenticate ()) {$this->adderror (' A_password ', ' username or password does not exist '); }}} Public function login () {if ($this->_identity = = = null) {$this->_identity = new Useridentity ($t      His->a_account, $this->a_password); $this->_identity->autHenticate (); } if ($this->_identity->errorcode = = = Useridentity::error_none) {$duration = $this->rememberme? 60*60*2      4*7:0;      Yii::app ()->user->login ($this->_identity, $duration);    return true;    } else {return false;       }} Public Function Attributelabels () {return array (' a_account ' = ' user name ', ' a_password ' = ' password '),  ' RememberMe ' = ' Remember login status ', ' verifycode ' = ' Verification code '; }}

Note: Indexform can also be written as LoginForm, but the system already has, I did not replace it, but also pay attention to the user table of the field, is generally password and username, and my is A_account and A_password

Controller


<?phpclass Indexcontroller extends Controller {public Function actions () {return array (' captcha ' = = Arra  Y (' class ' = ' ccaptchaaction ', ' width ' =>100, ' height ' =>50)); Public Function Actionlogin () {if (Yii::app ()->user->id) {echo <p> welcome. Yii::app ()->user->id. ", <a href= '". Site_url.    "Admin/index/logout ' > Exit </a></p>";      } else {$model = new indexform ();        if (Isset ($_post[' indexform ')) {$model->attributes = $_post[' Indexform ']; if ($model->validate () && $model->login ()) {echo <p> welcome. Yii::app ()->user->id. ", <a href= '". Site_url.        "Admin/index/logout ' > Exit </a></p>"; 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 a verification code to the

View


<meta http-equiv= "Content-type" content= "text/html;charset=utf-8" ><?php$form = $this->beginwidget (' Cactiveform ', array (' id ' = ' login-form ', ' enableclientvalidation ' = = True, ' clientoptions ' =& Gt Array (' validateonsubmit ' = true)); > <p class= "Row" > <?php echo $form->labelex ($model, ' a_account ');?> <?php Echo $form-&GT;TEXTF Ield ($model, ' a_account ');?> <?php echo $form->error ($model, ' a_account ');?> </p> <p class= "Row" > <?php echo $form->labelex ($model, ' A_password ')?> <?php echo $form->passwordfield ($model, ' A_pas Sword '),?> <?php echo $form->error ($model, ' A_password ');?> </p> <?php if (Ccaptcha::checkrequir Ements ()) {?> <p class= "Row" > <?php echo $form->labelex ($model, ' Verifycode ');?> <?php $this ->widget (' Ccaptcha ');?> <?php echo $form->textfield ($model, ' Verifycode ');?> <?PHP echo $form->error ($model, ' Verifycode ');?> </p> <?php}?> <p class= "Row RememberMe" > & lt;? PHP echo $form->checkbox ($model, ' rememberme '),?> <?php echo $form->label ($model, ' rememberme ');?> &L t;? PHP echo $form->error ($model, ' rememberme ');?> </p> <p class= "row Buttons" > <?php echo Chtml::su Bmitbutton (' Submit ');?> </p><?php $this->endwidget ();?>

Also modify the 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 ' demo '. * In practical applications, this should is 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_invali      D    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.