Yii Framework2.0 development tutorial (10) with mysql database for user login, yiiframework2.0

Source: Internet
Author: User

Yii Framework2.0 development tutorial (10) with mysql database for user login, yiiframework2.0

1. Create a table for saving users in mysql

Create table test_user (user_id bigint (20) unsigned not null auto_increment comment 'id', user_email varchar (100) not null comment 'email ', user_password varchar (100) not null comment 'Password', user_access_token varchar (200) comment 'Access _ token', user_auth_key varchar (200) comment 'auth _ key', user_create_time datetime comment 'creation time ', primary key (user_id ));

2. Insert a login account in the table



3. Create model models/MysqlUser. php

<? Phpnamespace app \ models; use yii \ db \ ActiveRecord; use yii \ web \ IdentityInterface; class MysqlUser extends ActiveRecord implements IdentityInterface {public static function tableName () {// corresponding table name return 'test _ user';} public static function findIdentity ($ id) {// $ temp = parent: find () is called during automatic login () -> where (['user _ id' => $ id])-> one (); return isset ($ temp )? New static ($ temp): null;} public static function findidentitybyaccesen en ($ token, $ type = null) {return static :: findOne (['user _ access_token '=> $ token]);} public function getId () {return $ this-> user_id;} public function getAuthKey () {return $ this-> user_auth_key;} public function validateAuthKey ($ authKey) {return $ this-> user_auth_key ===$ authKey;} public function validatePassword ($ password) {return $ this-> user_password ===$ password ;}}


4. Create a model models/MloginForm. php

<? Phpnamespace app \ models; use Yii; use yii \ base \ Model; // In addition, reference use app \ models \ MysqlUser; class MloginForm extends Model {public $ email; public $ password; private $ _ user = false; public function rules () {return ["email", "email ', 'message' => 'must be the mail format'], [['email ', 'Password'], 'required', 'message' => 'mandatory'], ['Password', 'validatepassword', 'message' => 'incorrect account or password '],];} // log on to the public function login () {if ($ this-> validate () re Turn Yii: $ app-> user-> login ($ this-> getUser (), 3600*24*30); elsereturn false ;} // determine whether the account and password are correct public function validatePassword ($ attribute, $ params) {if (! $ This-> hasErrors () {$ user = $ this-> getUser (); if (! $ User) {$ this-> addError ($ attribute, 'incorrect account or password ') ;}}// query the database public function getUser () by email address and password () {if ($ this-> _ user = false) {$ this-> _ user = MysqlUser: find () -> where (['user _ email '=> $ this-> email, 'user _ password' => $ this-> password])-> one ();} return $ this-> _ user ;}}?>


5. Create view views/accounts/login. php

<? Phpuse yii \ helpers \ Html; use yii \ widgets \ ActiveForm;?> <? Phpecho '


6. Create controller controllers/AccountController. php

<? Phpnamespace app \ controllers; use Yii; use yii \ filters \ AccessControl; use yii \ web \ Controller; use yii \ filters \ VerbFilter; // reference use app \ models \ MloginForm; class AccountController extends Controller {function actionLogin () {$ model = new MloginForm (); if ($ model-> load (Yii :: $ app-> request-> post () {if ($ model-> login () // return $ this-> renderPartial ('login ', ['model' => $ model, 'status' => 'success']); else // return $ this-> renderPartial ('login ', ['model' => $ model, 'status' => 'failed']);} else {return $ this-> renderPartial ('login ', ['model' => $ model, 'status' => '']) ;}}


In addition, the configuration for automatic login is config/web. php.



The effect is as follows:


Click the login button


If the account password is incorrect


Automatic Login has some problems and will be solved later.


Reprinted please indicate the source: http://blog.csdn.net/zhyoulun/article/details/40687545


In php, how does one read mysql database content and display it on the page using the yii framework technology? (The more detailed the better)

$ Datas = Article: model ()-> findAll ();

This is how data is obtained.

Article is the model name,
Foreach ($ datas as $ data ){
Echo $ data-> id;
Echo "<br/> ";
Echo $ data-> name;
Echo $ data-> xx;

}

How to Use yii for User Login

We recommend that you take a look at the blog instance tutorial ~ Give you a Chinese www.yiichina.org/doc/blog/prototype.auth
Then Yii Framework authenticates the identity and stores the user status www.yiichina.org/forum/thread-34-1-1.html
Bytes ~ Haha! Come on ~

I hope my answers will help you ~ (~ O ~)~ ZZ
Reference: www.yiichina.org

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.