Magento – Sharing Sessions Between Stores with Different Domains

cookiemultistoresession

I have three websites on a Magento installation, each has its own domain.

Now due to web-browser security you cannot access cookies from another domain, nor sessions.

Somehow I need to be able to pass temporary data between two different stores with different domains.

My specific use case:

I am using GeoIP to trigger a redirect to the correct countries store, however I also need to be able to manually override this. Now at the moment I am having the issue where I am manually overriding on the current store with a cookie, however when I am redirected to the other store, there is no cookie on the other store, so it falls back and looks up the users store based on GeoIP. However this then creates an infinity loop redirecting between too stores.

This is done by figuring out the website code for the store I want to use in index.php with a custom module and running Magento with the correct code. This handles all redirects etc for me then.

Best Answer

You can share frontend cookie between magento website with different domains using that solution ainixon.me/set-cookie-on-cross-domains.

You need to create cookies.php file with the following code

<?php
    setcookie("frontend", htmlspecialchars($_GET['SID']), time() + 86400);
?>

and in magento template you will need to add following code after the <body> tag

<?php 
$this_session_id = Mage::getSingleton('core/session', array('name' => 'frontend'))->getSessionId(); 
?>
<!-- setting cookies to other domains --> 
<img src="http://anotherdomain.com/cookies.php?SID=<?php echo $this_session_id; ?>" style="display:none;" />
<img src="http://somedomain.ne/cookies.php?SID=<?php echo $this_session_id; ?>" style="display:none;" /> 
<!-- setting cookies to others domains ends -->