Issue in setting cookie for subdomains in PHP

2020-02-14 php cookies shopify shopify-app

I tried to set cookies for embedded shopify app in php.

setcookie("user", '[email protected]', time()+3600, "/", "example.com", 1);

It works properly but if we are logged in with 2 shopify sites and if both opens the apps together in same browser, cookie value gets updated and same cookie gets set for both which causes same data to show in both places.

Answers

Cookie/Session works based on domain and will be unique.

suppose your app domain is example.com and you save logged in domain in session as

$_SESSION['logged_in_store'] = 'store1.myshopify.com'

when the second store login to your app this variable will simply updated as single session will work on this domain.

As a solution you can make use of wildcard subdomain

enable wildcard subdomain from your DNS.and when store login to your store redirect them to unique subdomain..... like store 1 will run on below subdomain

store1.your-app-domain.com

store 2 will run on

store2.your-app-domain.com

so on.. each store will run on different wildcard subdomain. and this will resolve your session / cookie conflicts.

Hope this will help.

Related