The original Yii only Admin and demo account, how can use the database inside the username and password landing it?
Database: For example, DatabaseName.
Data sheet: For example, TableName, the table contains username and password and other related information.
YII:
1. Turn on ' db ' =array (...) on main.php, open database connection (slightly)
2. Generate the model for the tablename.
3. Change the original account review in useridentity.php.
The code is as follows:
<?PHPclassUseridentityextendscuseridentity{parivate$_id;//define a private variable Public functionAuthenticate () {$username=strolower ($this-username);$user=user::model ()->find (' LOWER (username) =? ',Array($username));if($username===NULL){$this->errorcode=self::Error_username_invalid;}Else if(!$username->validatepassword ($this-password)) {$this->errorcode=self::Error_password_invalid;}Else{$this->_id=$user-ID;$this->username=$username-username;$this->errorcode=self::Error_none;}return $this->errorcode===self::Error_none;} Public functiongetId () {return $this-_id;}}
4. Add a ValidatePassword method to the user.php.
Public Function ValidatePassword ($password) {return $this->encrypt ($password) = = = $this->password;}
5. Add an encrypted Encrypt encryption method to the user.php:
Public Function ValidatePassword ($password) {return $this->encrypt ($password) = = = $this->password;}
In Ps:usercontroller:
' User ' =array (' * '); Any user
' User ' =array (' @ '); Registered users
' User ' =array (' admin ');
When you create account information in Ps:yii, the password you want to save in the database is MD5 encrypted:
Add a method to the user.php:
Public function ValidatePassword ($password) {return$this->encrypt ($ Password) = = =$thispassword;}