Yii user logon
Protected \ components \ UserIdentity. php file:
?
| 12345678910111213141516171819202122232425262728 |
ClassUserIdentityextendsCUserIdentity {private $ _ id; publicfunctionauthenticate () {$ record = User: model ()-> findByAttributes (array ('username' = >$ this-> username )); if ($ record = null) $ this-> errorCode = self: ERROR_USERNAME_INVALID; elseif ($ record-> password! = Md5 ($ this-> password) $ this-> errorCode = self: ERROR_PASSWORD_INVALID; else {$ this-> _ id = $ record-> id; $ this-> setState ('title', $ record-> title); $ this-> errorCode = self: ERROR_NONE;} return! $ This-> errorCode;} publicfunctiongetId () {return $ this-> _ id ;}} |
Model:
?
| 12345678910111213 |
$ Identity = newUserIdentity ($ this-> username, $ this-> password); if ($ identity-> authenticate () Yii: app () -> user-> login ($ identity); else echo $ identity-> errorMessage; // log out the current user Yii: app ()-> user-> logout (); // retain the user logon status for 7 days // ensure that the allowAutoLogin of the user part is set to true. // Yii: app ()-> user-> login ($ identity, 3600*24*7 ); |