Yii user with MySQL storage permission

Source: Internet
Author: User

Reference Link (The original Article is incorrect. This article has been corrected.)

 

Default table structure:

 Create   Table  Tbl_user (ID  Integer   Not   Null   Primary   Key  Auto_increment, username  Varchar ( 128 ) Not   Null , Password  Varchar ( 128 ) Not   Null  , Email  Varchar ( 128 ) Not   Null  ); 

 

Add the following encryption method to user. php (path models /):

 /*  ** @ Return Boolean validate user  */ Public   Function Validatepassword ( $ Password , $ Username  ){  Return   $ This -> Hashpassword ( $ Password , $ Username ) = $ This -> Password ;}  /*  ** @ Return hashed value  */ Public   Function Hashpassword ( $ Phrase , $ Salt = Null  ){  Define ('Sale _ length', 10 );  $ Key = 'Gf; B & yxl | bejuf-K * ppiu {WF | @ 9k9j5? D + YW }? Vazos % E2C-: 11ii <} ZM? Po! 96' ;  If ( $ Salt ='' ) $ Salt = Substr (Hash ('sha512 ', $ Key ), 0, Salt_length );  Else                  $ Salt = Substr ( $ Salt , 0, Salt_length );  Return Hash ('sha512 ', $ Salt . $ Key . $ Phrase );} 

 

Usercontroller. php (path controllers) change the method as follows (An error occurred in the original reference link.)

 /*  ** Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page.  */  Public   Function  Actioncreate (){  $ Model = New  User;  // Uncomment the following line if Ajax validation is needed // $ this-> using majaxvalidation ($ model );          If ( Isset ( $ _ Post ['User' ]) {  $ Model -> Attributes = $ _ Post ['User' ];  $ Model -> Password = $ Model -> Hashpassword ( $ _ Post ['User'] ['Password'],$ _ Post ['User'] ['username' ]);  If ( $ Model -> Save ())  $ This -> Redirect ( Array ('View', 'id' => $ Model -> ID ));  Else                          $ Model -> Password = $ _ Post ['User'] ['Password'];}  $ This -> Render ('create ', Array  ( 'Model' => $ Model , ));} 

 

Change "useridentity. php" (Path Components) as follows

 

 Public   Function  Authenticate (){  $ Username = $ This -> Username; $ User = User: Model ()-> Find ('username =? ', Array ( $ Username  ));  If ( $ User === Null  )  $ This -> Errorcode = self :: Error_username_invalid;  Else   If (! $ User -> Validatepassword ($ This -> Password, $ This -> Username ))  $ This -> Errorcode = self :: Error_password_invalid;  Else  {  $ This -> Username = $ User -> Username;  $ This -> Errorcode = self :: Error_none ;} Return ! $ This -> Errorcode ;} 

 

End

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.