Opencart cart across multiple stores with different subdomains

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 21:15:53

OpenCart stores all these information in you PHP session. Since your stores are located under different subdomains, the PHP session changes when you switch from one store to another.

So the first thing you need to do is to share the session between all subdomains. By default, PHP uses the 'PHPSESSID' cookie to propagate session data across multiple pages, and by default it uses the current top-level domain and subdomain in the cookie declaration.

Example: www.domain.com

The downside to this is that the session data can't travel with you to other subdomains. So if you started a session on www.domain.com, the session data would become unavailable on forums.domain.com. The solution is to change the domain PHP uses when it sets the 'PHPSESSID' cookie.

Assuming you have an init file that you include at the top of every PHP page, you can use the ini_set() function. Just add this to the top of your init page:

ini_set('session.cookie_domain',
substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));

This line of code takes the domain and lops off the subdomain.

Example: forums.domain.com -> .domain.com

Now, every time PHP sets the 'PHPSESSID' cookie, the cookie will be available to all subdomains!

You might also need to make some little modifications to the OpenCart's core in order to make it work.

Have fun :)

After Tohids help I have the following solution, hopefully it helps others. I added the cookie_domain code line to the session.php file and also added or changed the cookie name wherever the setcookie function was used to cover the currency and language cookies.

open \system\session.php

find;

ini_set('session.use_cookies', 'On');
ini_set('session.use_trans_sid', 'Off');

insert after;

ini_set('session.cookie_domain', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));

open \index.php

find;

if (!isset($request->cookie['language']) || $request->cookie['language'] != $code) {      
  setcookie('language', $code, time() + 60 * 60 * 24 * 30, '/', $request->server['HTTP_HOST']);
}

replace with;

if (!isset($request->cookie['language']) || $request->cookie['language'] != $code) {      
  setcookie('language', $code, time() + 60 * 60 * 24 * 30, '/', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));
}           

find;

if (isset($request->get['tracking']) && !isset($request->cookie['tracking'])) {
    setcookie('tracking', $request->get['tracking'], time() + 3600 * 24 * 1000, '/');
}

replace with;

if (isset($request->get['tracking']) && !isset($request->cookie['tracking'])) {
  setcookie('tracking', $request->get['tracking'], time() + 3600 * 24 * 1000, '/', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));
}

open system\currency.php

find;

if (!isset($this->request->cookie['currency']) || ($this->request->cookie['currency'] != $currency)) {
  setcookie('currency', $currency, time() + 60 * 60 * 24 * 30, '/', $this->request->server['HTTP_HOST']);
}

replace with;

if (!isset($this->request->cookie['currency']) || ($this->request->cookie['currency'] != $currency)) {
  setcookie('currency', $currency, time() + 60 * 60 * 24 * 30, '/', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));
}

Very Easy Solution! Share the login session across the subdomains

  1. OPEN FILE: system/library/session.php
  2. FIND LINE: session_set_cookie_params(0, '/');
  3. APPEND : session_set_cookie_params(0, '/','.DOMAIN.COM);

Make sure to include the period "." before DOMAIN.COM

That's it... Now login sessions started on www.domain.com is shared with www.sub.domain.com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!