9

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.

1
  • Are your store views placed in different websites? I mean websites in the terminology of Magento. If yes, you may also need to read this article, that explains, how to create a simple extension, that shares the shopping cart between different Magento websites: marius-strajeru.blogspot.com/2013/01/…
    – ceckoslab
    May 10, 2013 at 18:00

4 Answers 4

5

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 -->
2
  • Hi your solution awesome. But i'm a little confused about the magento 2.1.3 version. so where should i enter this code in this new version?
    – Zammuuz
    Jan 26, 2017 at 9:16
  • Some security implications of this suggestion were discussed here security.stackexchange.com/questions/174678/…
    – Goose
    Dec 1, 2017 at 18:11
3

Just redirect with ?SID=yourSessionId then you can just use it on the other store view.

10
  • Magento doesn't have a configuration to enable or not the SID in the urls? I think there is something in Configuration > Web > Session. No? May 10, 2013 at 14:02
  • yes, when you redirect between storeviews. there is something, but I don't think this works if you do it manually. But yes Luke, have a look into this in the backend and the consequensted, might make the life easier :) May 10, 2013 at 14:04
  • This is already enabled, but I does not work for me by default. The module we created is designed to pull a location based on IP and then direct accordingly, and also allow people to select stores. I am simply working out the mage_run_code before Mage::run is initially called. But I do not know how I can add a custom session value before mage run and also have the session id appended to the url redirect, as this is handled in core/url
    – lukefowell
    May 10, 2013 at 14:18
  • just search the code for SID. Everything happens here: \Mage_Core_Model_Session_Abstract::setSessionId() so just check when the session id is set and set it. May 10, 2013 at 14:26
  • 1
    You might want to be more specific when suggesting this. If the SID is appended to a URL available from a link, the user could share that link on a forum or elsewhere which would cause you all sorts of problems.
    – pspahn
    Oct 9, 2014 at 16:29
2

Just ensure Use SID in frontend is enabled.

Any ...

  • URL generated by Mage::getUrl()
  • Redirect with $this->_redirectUrl($url); or Mage::app()->getFrontController()->getResponse()->setRedirect()

Will then contain the respect SID.

If you are using files for session storage, just make sure the directory is commonly symlinked; if you are using Memcache/Redis/DB, this isn't a concern.

--

Just note, there is a lot of graceful ways to handle GeoIP redirection further up than Magento. You could do it at the webserver level (checking the presence of your respective "no-redirect" cookie).

2
  • I have found that it is including the SID on the end of the URL initially but it keeps redirecting around in circles. What I want to do is when I manually redirect set a session value to stop it looking up the IP again next time to send it back to where it came from. However I get an error like "Mage registry key "controller" already exists" when ever I try to access any factory methods in magento or retrieve session data etc. So I am trying to find a way to pass a flag to say "no don't lookup again please". Any ideas?
    – lukefowell
    May 10, 2013 at 16:02
  • You'll have a redirect loop just because of your own code. There's no real way I can help you on here, your issue is far too localised. May 10, 2013 at 16:41
0

In your admin, go to Configuration > Web > Session > Use SID on Frontend and enable.

When going from one site to another, pass the session by including this in the url (use & if you already use ? for a $_GET parameter)

?SID=<?php echo Mage::getSingleton('core/session', array('name' => 'frontend'))->getSessionId(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.