You are viewing an old revision of this post, from April 25, 2017 @ 11:24:11. See below for differences between this version and the current revision.

Magento – Login Into Multiple Website Store

Problem Case:

The problem we are trying to solve here is, suppose you have multiple magento website setup on different domains on the same magento instance, and you want that if a customer login’s on any one our your site he is gets logged into all other sites as well.

Solution:

The solution for this is simple, whenever a user logs in on a website ‘A’, a script needs to notify website ‘B’,’C’, etc that user has logged in and then login needs to be performed in ‘B’,’C’, etc as well. This cannot be handled automatically because session is not shared across domains so this feature is not there by default. The solution for this is to use JSONP and perform a cross domain ajax request with the session id so that session gets shared accross domains.
Attached is the source code of the module
[dm]33[/dm]

Implementation

First Step: Magento has a default configuration option called Share Customer Accounts located at System -> Configuration -> Customer Configuration -> Account Sharing Options, change this to Global. What this means is that when a customer creates his new account, is shared for all your websites.
Second Step:
Next we need to write code to make the JSONP calls. We are using JSONP, since normal cross domain ajax calls are not allowed. For this we are using JQuery library , details of JSONP can be found here.

So below is the codes:

Step1: Include jQuery in Magento

First download jQuery and then place it inside /js/jquery folder, so path would be /js/jquery/jquery.js. Next create a javascript file called noconflict.js in the jquery folder (/js/jquery/noconflict.js) jQuery Folder Structure. Write this code inside noconflict.js file

1

jQuery.noConflict();

Next open that page.xml layout file in your theme folder [app/design/frontend/default/YOUR_THEME/layout/page.xml or in your default magento theme] and place this code inside tag

1

2

3

4

<reference name="head">

            <action method="addItem"><type>js</type><name>jquery/jquery-1.6.4.min.js</name></action>

            <action method="addItem"><type>js</type><name>jquery/noconflict.js</name></action>

</reference>

Now open home page in your magento, and through firebug or chrome inspector, see if these two jquery files are included in your page.

Step2: Crossdomain ajax call

Next we need to write ajax call. The code for this should be written in header.phtml. You need to rewrite the base header.phtml in your theme [app/design/frontend/default/YOUR_THEME/page/html/header.phtml]
Here’s the added code for header.phtml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

<?php

$allStores=Mage::app()->getStores(); //get list of all stores,websites

     

    foreach ($allStores as $_eachStoreId => $val)

      {

        $_storeId = Mage::app()->getStore($_eachStoreId)->getId();

         $urls= Mage::app()->getStore($_storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB); //get url using store id

         

         $cururl=Mage::getStoreConfig('web/unsecure/base_url'); //get current domain url

        if($cururl!=$urls)

        {

             $url=$urls;

             

         if ($this->helper('customer')->isLoggedIn()){

          if(Mage::getSingleton('core/session')->getLogincd()==1){ ?>

            <script type="text/javascript">

            jQuery(document).ready(function() {

            var url="<?php echo $url;?>customer/account/logincd?SID=";

             

            url = url+'<?php echo Mage::getModel("core/session")->getEncryptedSessionId(); ?>'+"&callback=?";

            var userid=<?php echo Mage::getSingleton('customer/session')->getCustomer()->getId();?>;

            jQuery.ajax({

            url: url,

            crossDomain: true,

            dataType: 'jsonp',

            success: function(data) {

                //console.log("success");

            }

            });

        });

        </script>

        <?php

         

        }

        Mage::getSingleton('core/session')->setLogincd(0);

         }else{

        Mage::getSingleton('core/session')->setLogincd(1);

         }

        }

     

    }

 

    ?>

Here first we are getting all stores, then in a foreach loop we are getting the domain url of each store. Then we are checking if the current domain is not equal to the fetched url. Then we perform the jsonp call which will go to the other domain logincd Action and login the customer on the other domain. For this we are sharing the customer session id i.e. SID with other domains. We get the session id of the logged in customer with the code

1

Mage::getModel("core/session")->getEncryptedSessionId();

We are appending session id in the url and this will be set in other domain and the customer will be logged in.
Now let us see how ‘jsonp’ request is different from normal ajax request.

1

url: "http://domain1.excellencetenchnologies.co.in/logincd/?SID=hjdsk3242shjhs&callback=?",

In this line we we are appending the session id of the customer session of the current domain, and a ‘callback’ parameter which will be generated by jQuery.
It is a unique callback name for this request (something like json1268267816). It will be received at the other domain and it will treat the request as jsonp.

Mage::getSingleton(‘core/session’)->getLogincd() parameter is set so that this jsonp call is done only once and not every time a page is opened.

Step3: Overwrite the AccountController.php of Mage/Customer/controllers/

Next we need to overwrite AccountController.php file in our module [app/code/local/YOUR_NAMESPACE/MODULE_NAME/controllers/AccountController.php]. The AccountController.php file will have an action ‘logincd’ which will receive the ajax call and then do all the work required to make customer login in the domain. Here’s the code for AccountController.php

1

2

3

4

5

6

7

8

9

10

11

12

13

<?php

require_once 'Mage/Customer/controllers/AccountController.php';

     

class Excellence_Logincd_AccountController extends Mage_Customer_AccountController

{

    public function logincdAction()

    {

    $this->getResponse()->setHeader('Content-Type''application/json; charset=utf-8');

        $data array('success'=>1);

        echo $_GET['callback'] . '('.json_encode($data).')';

     }

    

}

This line designates the content to be in json format, encoded in the UTF-8 character encoding.

1

this->getResponse()->setHeader('Content-Type''application/json; charset=utf-8');

And then the success message has been set and sent back to the calling function.

1

echo $_GET['callback'] . '('.json_encode($data).')';

this will send back the response to the requesting ajax call as jsonp.

This is all that is required to make cross domain login work. The cross domain logout will work automatically in the current case.

Revisions

  • April 25, 2017 @ 11:24:11 [Current Revision] by Sharing Solution
  • April 25, 2017 @ 11:24:11 by Sharing Solution

Revision Differences

There are no differences between the April 25, 2017 @ 11:24:11 revision and the current revision. (Maybe only post meta information was changed.)

2 Responses to “Magento – Login Into Multiple Website Store”

  1. Kelvin Seelye 10/09/2017 at 10:33 pm #

    I have been reading out many of your articles and i must say pretty clever stuff. I will make sure to bookmark your blog.

  2. Roscoe Linenberger 13/09/2017 at 7:38 pm #

    Thankyou such a lot of with respect to stating this great post. The country’s extremely a good choice for me. Awesome position!

Leave a Reply