SSO (single sign-on) is the name of a number of sub-projects that share one login point. The principle is simply that server session sharing, client cross-domain cookies.
The implementation is very simple, protected/config/main.php the session configuration can be modified
The code is as follows:
1 $host=Explode(‘.‘,$_server["Http_host"]);2 if(Count($host) > 2) {3 Define(' DOMAIN ',$host[1]. ‘.‘ .$host[2]);4}Else {5 Define(' DOMAIN ',$host[0]. ‘.‘ .$host[1]);6 }7 8 9' Components ' =Array(Ten' User ' =Array( One' Allowautologin ' =true, A' Autorenewcookie ' =true, - //' class ' = ' WebUser ', -' Statekeyprefix ' = ' xxx ', the), -' Session ' =Array( -' Savepath ' = ' C:\session_temp ',//dirname (__file__). Directory_separator. ‘..‘ . Directory_separator. ‘..‘ . Directory_separator. ' Session_temp ',//' C:\session_temp ',// -' Cookieparams ' =Array(' domain ' = '. '). DOMAIN, ' lifetime ' = 0), +),
*****
The above code is implementation code, YII configuration is simple,
Problem
One. Open ' class ' = ' WebUser ', error
Include (webuser.php): Failed to open stream:no such file or directory
This is because the WebUser class is not defined, and a new webuser.php is created under the Protected\components directory, as follows:
1<?PHP2 3 //This file must is stored in:4 //protected/components/webuser.php5 6 classWebUserextendsCwebuser {7 8 //Store model to not repeat query.9 Private $UserLogin;Ten One //Return First name. A //Access it by Yii::app ()->user->first_name - functionGetfirst_name () { - $user=$this->loaduserlogin (Yii::app ()->user->user_id); the return $user-first_name; - } - - //This was a function that checks the field ' role ' + //In the User model to being equal to 1, that's means it ' s admin - //Access it by Yii::app ()->user->isadmin () + functionISAdmin () { A $user=$this->loaduser (Yii::app ()->user->user_id); at return intval($user->user_role_id) = = 1; - } - - //Load user model. - protected functionLoaduserlogin ($id=NULL) - { in if($this->userlogin===NULL) - { to if($id!==NULL) + $this->userlogin=userlogin::model ()->FINDBYPK ($id); - } the return $this-Userlogin; * } $}?>
Two. Even though it is configured, the subdomain still shows no sign-in.
This is because the session save location is not in the same place, the Savepath changed to the same place can be
Before modification:
1 ' savepath ' =dirname(__file__
After modification:
1 ' Savepath ' = ' C:\session_temp ',
Yii SSO implementation with single sign-on with domain name