Yii2 enables synchronous logon and exit for multiple domain names across domains, and yii2
During platform development, the project is divided into two parts: frontend www.xxx.com and backend yun.xxx.com. Bind two domain names, we know that the frontend and backend can log on and exit synchronously when the domain name is not bound, but it becomes invalid after the domain name is bound, because the session has different scopes. The session scopes of both domain names are limited to their own domain names. Our solution is to change the scopes of different second-level domain names to the top-level domain name xxx.com.
Add the following code in common/config/main. PHP:
// Configure the cross-origin session domain name to obtain the current host name $ host_array = explode ('. ', $ _ SERVER ["HTTP_HOST"]); // obtain the top-level DOMAIN name if (count ($ host_array) = 3) {define ('domain ', $ host_array [1]. '. '. $ host_array [2]);} // For the com.cn DOMAIN name elseif (count ($ host_array) = 4) {define ('domain ', $ host_array [1]. '. '. $ host_array [2]. '. '. $ host_array [3]);} else {// echo "the system does not support local access. Please configure the DOMAIN name"; exit;} define ('domain _ home', 'www. '. DOMAIN); define ('domain _ YUN ', 'yun. '. DOMAIN); define ('domain _ api', 'api. '. DOMAIN); define ('domain _ EMAIL ', 'mail. '. DOMAIN); define ('domain _ IMG ', 'img. '. DOMAIN );
Modify the components part and change the session scope.
'user' => [ 'identityClass' => 'common\models\User', 'enableAutoLogin' => true, 'identityCookie' => ['name' => '_identity', 'httpOnly' => true, 'domain' => '.' . DOMAIN], ], 'session' => [ 'cookieParams' => ['domain' => '.' . DOMAIN, 'lifetime' => 0], 'timeout' => 3600, ],
After the above configuration, multiple second-level domain names can achieve synchronous login and exit.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.