In magento it is not possible to check session data unless Mage::run is initilaized. We need to do some temporary storage like cookie to achieve this. Hopefully the below steps will make it works.
Create a custom module with observer for saving, clearing and checking cookie. Your config.xml file is given below
<!--------------------------------------------------------------------->
<customer_login>
<observers>
<Company_Modulename_model_customer_login>
<class>Company_Modulename_Model_Observer</class>
<method>afterCustomerLoggedIn</method>
</Company_Modulename_model_customer_login>
</observers>
</customer_login>
<customer_logout>
<observers>
<Company_Modulename_model_customer_logout>
<class>Company_Modulename_Model_Observer</class>
<method>afterCustomerLoggedOut</method>
</Company_Modulename_model_customer_logout>
</observers>
</customer_logout>
<controller_action_layout_load_before>
<observers>
<Company_Modulename_model_layoutload_observer>
<class>Company_Modulename_Model_Observer</class>
<method>beforeLoadLayout</method>
</Company_Modulename_model_layoutload_observer>
</observers>
</controller_action_layout_load_before>
<!--------------------------------------------------------------------->
Observer implementation is given below
public function afterCustomerLoggedIn(Varien_Event_Observer $observer)
{
$customer = $observer->getCustomer();
$cid = $customer->getid();
$webId = $customer->getWebsiteId();
Mage::getModel('core/cookie')->set('_storeuid',$cid, 60*60*24*1); // Create cookie
Mage::getModel('core/cookie')->set('_storeid',$webId, 60*60*24*1); // Create cookie
}
public function afterCustomerLoggedOut(Varien_Event_Observer $observer)
{
$customer = $observer->getCustomer();
$cid = $customer->getid();
$webId = $customer->getWebsiteId();
Mage::getModel('core/cookie')->set('_storeuid','0', 1); // Clear cookie
Mage::getModel('core/cookie')->set('_storeid','0', 1); // Clear cookie
}
Before rendering pages
public function beforeLoadLayout(Varien_Event_Observer $observer)
{
$cookieValue = Mage::getModel('core/cookie')->get('_storeuid');
$baseurl = Mage::getBaseUrl();
if(Mage::getSingleton('customer/session')->isLoggedIn()){ // For more security , if someone change the cookie value
$customer = Mage::getModel('customer/session')->getCustomer();
$webId = $customer->getWebsiteId();
$cid = $customer->getId();
if( $cookieValue != $cid ){
Mage::getModel('core/cookie')->set('_storeuid',$cid, 60*60*24*1);
Mage::getModel('core/cookie')->set('_storeid',$webId, 60*60*24*1);
header("Location: ".$baseurl); exit;
}
}else{
if( (isset($cookieValue)) && ($cookieValue > 0) ){ // Clear cookie if session is cleared
Mage::getModel('core/cookie')->set('_storeuid','0', 1);
Mage::getModel('core/cookie')->set('_storeid','0', 1);
header("Location: ".$baseurl); exit;
}
}
}
And Finally in index.php , you can check the cookie for switching the store value for loggedin customers
$store_id = 1;
if( (isset($_COOKIE['_storeur'])) && ($_COOKIE['_storeur'] > 0) ){
$store_id = $_COOKIE['_storeur'];
}
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
if($store_id == 2 ) $mageRunCode = 'store2';
if($store_id == 3 ) $mageRunCode = 'store3';
//....................
Mage::run($mageRunCode, $mageRunType);