Yii User Login verification (cwebuser) yii user Login (Remember)

Source: Internet
Author: User
Tags yii

Yii is really a powerful and bloated framework, simple small projects, or only the background interface calls the project, not recommended.

Today record the use of Yii Cwebuser (Yii::app ()->user->login ()) login;

1. Preparatory work

Official standard practice, add a useridentity.php file to the components, class Useridentity extends Cuseridentity

Inherit the cuseridentity, and then write the validated business process in the Useridentity class (such as which table you are in to check data, fetch data, etc.)

2. Start Login

New a useridentity, passing in parameters, and then executing the validation method

Cases:

$this->_identity=new useridentity ($this->username, $this->password);
$this->_identity->authenticate ();

3. Call Yii::app ()->user->login () to save the data (session)

Calling Yii Component Settings session

Yii::app ()->user->login ($this->_identity, $duration);//$duration, Session expiration time, this method is/framework/web/auth/ cwebuser.php Line 222

$this the->_identity Object GetID (), the GetName () method generally returns the user name, user ID, etc.

The official Cuseridentity class, are all returned user name ($this->username)//This method in/framework/web/auth/cuseridentity line 78

If UID is required, you can rewrite the GetId () method in useridentity, so that when you use Yii::app ()->user->getid (), the UID is called instead of username.

This allows access to the user data in the session via Yii::app ()->user->getid (), Yii::app ()->user->getname ().

If you need to put additional data, rewrite the Getpersistentstates () method in the Cuseridentity class, and return the data you want to save in the form of data.

Here you can go through Yii::app ()->user->getstate (' key '); This form of call, where key is the Getpersistentstates () method return out of the array key.

This article describes a Yii user authentication idea, do not recommend copying any one of the text code. You can do it yourself after reading comprehension. Have basic skill can see/framework/web/auth/cwebuser.php source

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.