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