Yii2session cross-domain coexistence solution in Yii2

Source: Internet
Author: User

Yii2session cross-domain coexistence solution in Yii2

I have talked about session-related configurations before. during development, the logon module is often required for cross-origin sharing sessions. I believe many developers have encountered this issue and only need to log on to the same area, the associated website is also logged on. Two cases: one is between 9streets.cn and a.9streets.cn, and the other is between a.com B .com. The processing methods are summarized over the past few days.

Whether it is a level-1 domain name or a cross-domain name under a different domain name, there are only two points:

  • The client accesses the same sessionId,
  • The Data Location of the session accessed by the server corresponding to all domain names must be consistent.

1. To access the common sessionId, write the current sessionId into the cookie.

Cookie cannot be accessed under different domain names. We need to set the Domain Name of the login information to be shared when users log on at the background during access. If the cookie is under level 1 or 2 domain names, directly set the cookie to the primary domain name, for example:

setcookie("session_id",session_id(),time()+3600*24*365*10,"/",".a.com"); 

Maybe you will ask: What if it is in different domain names? The P3P technology is used for simple solution and implementation. When you access x.com, The y.com program triggers the write sessionid value to the y.com file, and the sessionid value can be obtained, and then the seesion value is saved to the database, take the same sessionid value. This requires that the program files in y.com must be accessible across domains. By default, the browser cannot set cookies across domains, and the p3p header must be added. Add the following to the corresponding PHP file:header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');

2. Implementation of consistent session data storage locations

Session: the data is stored in the tmp file of the server by default. It exists as a file rather than in the memory of the server, here we have to change it to a method that can be accessed in all domains. I have introduced database storage, file storage, and memory storage on the Internet. If session data is stored in a database and the website has a large amount of access traffic, the SESSION metadata describes the data inventory session instances.

To solve this problem in yii2, the final configuration successful tutorial is as follows:

Configure in main. php In the config folder of frontend

$host = explode('.', $_SERVER["HTTP_HOST"]);if (count($host) > 2) {  define('DOMAIN', $host[1] . '.' . $host[2]);} else {  define('DOMAIN', $host[0] . '.' . $host[1]);}define('DOMAIN_HOME', 'www.' . DOMAIN);define('DOMAIN_USER_CENTER', 'man.' . DOMAIN);define('DOMAIN_API', 'api.' . DOMAIN);define('DOMAIN_EMAIL', 'mail.' . DOMAIN);define('DOMAIN_LOGIN', 'login.' . DOMAIN);define('DOMAIN_IMG', 'img.' . DOMAIN);

Then configure the User and Session:

'user' => [  'enableAutoLogin' => true,  'identityCookie' => ['name' => '_identity', 'httpOnly' => true, 'domain' => '.' . DOMAIN],],'session' => [  'cookieParams' => ['domain' => '.' . DOMAIN, 'lifetime' => 0],  'timeout' => 3600,],

Here, in the user and session of the configuration item, I have killed the domain, for example, 'domain '=>' .baidu.com ', so that no judgment is needed.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.