This time to everyone to bring YII2 to achieve single sign-on, Yii2 realize single sign-on note what, the following is the actual case, together to see.
This article introduces the method of realizing single sign-on in Yii2, and share it with you, as follows:
Modify/common/config/main.php
First, add the following code to the config header
<?php//Session cross-domain $host = explode ('. ', $_server["Http_host"]), if (count ($host) > 2) { define (' DOMAIN ', $host [1 ] . '.' . $host [2]);} else { define (' DOMAIN ', $host [0]. '.' . $host [1]);}
Second, add the config in the configuration
<?php ' user ' = [ ' identityclass ' + ' common\models\user ', ' enableautologin ' and ' = True ' Identitycookie ' + ' = [' name ' = ' _identity ', ' httponly ' = ' + ', ' domain ' = '. '. Domain], ' session ' = [ ' cookieparams ' and ' = ' domain ' = '. DOMAIN, ' lifetime ' = 0], ' timeout ' = 3600,],
Third, use of controller
<?php//set Yii:: $app->session[' var ']= ' value ';//Use echo YII:: $app->session[' var '];//remove unset (Yii:: $app- Session[' var ');
Iv. Testing
4.1 www.aaa.com Landing
4.2 www.bbb.com session still works.
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
How PHP generates random numbers
How to use the recursive function of PHP