This article mainly introduces how Yii2 achieves multi-domain name cross-domain synchronous login and exit. I think this is quite good. I will share it with you and give you a reference. Let's take a look at the development process of the platform. The project is divided into two parts: frontend www.xxx.com and backend yun.xxx.com, and two domain names are bound, 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 help you learn and support PHP.
For more information about Yii2 implementing multi-domain name cross-domain synchronous login and exit, please follow the PHP Chinese network!