Yii2 enables synchronous logon and exit for multiple domain names across domains, and yii2

Source: Internet
Author: User

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.

Related Article

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.