In this Wiki, you can learn how to use a database to create a front-end and back-end login forms.
In this Wiki, you can learn how to use a database to create a front-end and back-end login forms.
Application scenarios
After creating the default yii project for beginners, you need to modify the static logon form to dynamic, that is, read the user name and password from the database.
Default settings
I have referenced this article to build the project's foreground and background directory structure. Thanks to Andy for this article. By following all the steps, you will have a separate front-end and rear panel, such:
- Http: // localhost/index. php // foreground
- Http: // localhost/backend. php // background management
I used two different data models to process front-end and back-end user databases.
- User
- Admin user
LoginForm
Path: models/LoginForm. php
Here, I have added a new variable for this class, that is, userType (user type)
Class LoginForm extends CFormModel {public $ username; public $ password; public $ rememberMe; public $ userType; // add the new member variable private $ _ identity; public function _ construct ($ arg = 'front') {// Set to foreground $ this-> userType = $ arg;} by default ;} // === Other codes === public function authenticate ($ attribute, $ params) {if (! $ This-> hasErrors () {$ this-> _ identity = new UserIdentity ($ this-> username, $ this-> password ); $ this-> _ identity-> userType = $ this-> userType; // this will pass the identifier to UserIdentify if (! $ This-> _ identity-> authenticate () $ this-> addError ('password', 'recorrect username or password .');}}
UserIdentity:
Path: components/UserIdentity. php
Add new member variables like LoginForm
UserType = 'front') // only the Front-end login {// check whether the login information exists in the database $ record = User: model () -> findByAttributes (array ('username' => $ this-> username); if ($ record = null) {$ this-> errorCode = self :: ERROR_USERNAME_INVALID;} else if ($ record-> password! ==$ This-> password) // compare the password in the data with the password in the logon information {$ this-> errorCode = self: ERROR_PASSWORD_INVALID ;} else {$ this-> setState ('userid', $ record-> userId); $ this-> setState ('name', $ record-> firstName. ''. $ record-> lastName); $ this-> errorCode = self: ERROR_NONE;} return! $ This-> errorCode;} if ($ this-> userType = 'back ') // Here is the background management logon {// check whether the logon information exists in the database $ record = AdminUser: model () -> findByAttributes (array ('email '=> $ this-> username); // Here I use the mailbox in the database as the user name if ($ record = null) {$ this-> errorCode = self: ERROR_USERNAME_INVALID;} else if ($ record-> password! = Base64_encode ($ this-> password) // Obtain the base64_encode encrypted password and compare the password in the database {$ this-> errorCode = self: ERROR_PASSWORD_INVALID ;} else {$ this-> setState ('isadmin', 1); $ this-> setState ('userid', $ record-> userId ); $ this-> setState ('name', $ record-> name); $ this-> errorCode = self: ERROR_NONE;} return! $ This-> errorCode ;}}}
Code in Action:
All the settings have been completed. The next step is to use the LoginForm object in the controller.
Path: Controllers/Front/SiteController. php
$ Model = new LoginForm ('front'); // use the frontend logon form of the 'user' model
Path: Controllers/Back/SiteController. php
$ Model = new LoginForm ('back'); // use the background logon form of the 'adminuser' model
You can also find some excellent articles about user access management. However, as a beginner, I think this code can help you. Share your thoughts and comments.
Happy coding! :)
This article translated from foreign language website, view the original, please click: http://www.yiiframework.com/wiki/356/how-to-create-front-and-admin-side-login-form