YII2 implementing multi-Domain synchronization login exit

Source: Internet
Author: User
In the platform development process, the project is divided into the foreground (frontend) www.xxx.com and Backstage (backend) yun.xxx.com two parts, binding two domain names, we know that there is no binding domain name when the front and back can be synchronized login and exit, but the binding domain name after the failure, The reason is that the scope of the session is different. Two domain names of the session scope are limited to their own domain name, our solution is to change the scope of different two domain names to the top-level domain name xxx.com.

In Common/config/main. Add the following code inside PHP:

Cross-domain session domain name configuration, get the current hostname $host _array = explode ('. ', $_server["Http_host"]); For COM domain names, get the top-level domain if (count ($host _array) = = 3) {   define (' domain ', $host _array[1]. '.' . $host _array[2]); }//For com.cn domain name ElseIf (count ($host _array) = = 4) {   define (' domain ', $host _array[1]. '.' . $host _array[2]. '.' . $host _array[3]); } else{   //echo "This 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 components to change session scope

' User ' = [       ' identityclass ' + ' common\models\user ',       ' enableautologin ' and ' = True       ' Identitycookie ' + ' = [' name ' = ' _identity ', ' httponly ' = ' + ', ' domain ' = '. '. Domain],     ' session ' = [       ' cookieparams ' and ' = ' domain ' = '. DOMAIN, ' lifetime ' = 0],       ' timeout ' = 3600,     ],

With the above configuration, multiple level two domain names can be used to synchronize logins and exits.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

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.