Browsing articles tagged with "session Archives - Kalpesh Mehta"
Sep 2, 2014
kalpesh

Magento set session cookie lifetime to 1 day

Is your Magento shopping cart kicking out all the items after 24 minutes? Are you frustrated because your customer’s cart items are getting flushed every few hours even though you have correct setting in Magento? Do you want to increase your shopping cart sessions to last for 1 day (or anything you wish)?

Here are the things to look for to increase/decrease Magento’s session cookie lifetime.

In your php.ini file:

1
session.gc_maxlifetime 86400

If you have not changed this, it should be by default 1440 i.e. 24 minutes. Change it to 86400 for one day session lifetime

In your Magento admin:

1
2
System -> Configuration -> Checkout -> Shopping Cart
Quote Lifetime (days) -> 1
1
2
System -> Configuration -> Web -> Session Cookie Management
Cookie Lifetime -> 86400

Make sure to check if you are in correct website/store if using multi-website/multi-store Magento setup. You can change the scope by changing the website/store from the upper left Store Switcher drop down in System -> Configuration screen.

If you are changing php.ini value, make sure you reload apache to reflect the changes.

Nov 3, 2013
kalpesh

Magento remove session id from URL

Magento displays session ID in url, something like:

1
http://loca.lho.st?__SID=2wewesfdgfsdm

You can remove this in two ways:

1. Go to your Magento admin panel > System > Configuration > Web.
Under Session Validation Settings, set “No” against label “Use SID on the Frontend”.
If this doesn’t work, then move to option two below.

2. Edit the file at app/code/core/Mage/Core/Model/App.php (somewhere around line 222),

1
protected $_useSessionInUrl = true;

Change that value to “false”. That should now prevent session IDs appearing in URL.

Jun 19, 2011
kalpesh

Magento: Get and set variables in session

Suppose you want to set data in the magento session, so that you can retrieve it from other pages. Session has been very useful global variable in PHP and using it we can easily retrieve and add data to it and get it from the entire site.

Here is the code that will set / add your custom data into session.

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

here, setMyCustomData can be replaced with the meaningful name as per your need. One thing to be cautious while setting session variables is to make sure it doesn’t conflict with magento’s internal variables, example setData.

Code to retrieve the session variable

1
2
$session = Mage::getSingleton('core/session', array('name' => 'frontend'));
echo $session->getMyCustomData();
Jun 19, 2011
kalpesh

Magento: Checking customer/admin is logged in or not

The most important part in magento development is to check whether customer is logged in or not in the system. So that you can show specific features and extra privileges to your customers. There are many things that are restricted to the guests, while members can see the special privilege things.

Also if the customer is not logged in, we can show a link like “Log in”; while for logged in customers, we can show link like “Log out”, “My account”, etc. This is already present in Magento, but we may want to add more features to our customers. So here is the code where you can check customer is logged in or not.

This will check whether the customer is logged in the magento system or not.

1
2
3
4
5
6
7
$session=Mage::getSingleton('customer/session', array('name'=>'frontend') );

if ($session->isLoggedIn()) {
    echo "Welcome, ".$session->getCustomer()->getName();
} else {
    echo "Not logged in";
}

This will check whether admin is logged in or not in a magento site.

1
2
3
4
5
6
7
$adminsession = Mage::getSingleton('admin/session', array('name'=>'adminhtml'));

if($adminsession->isLoggedIn()) {
    echo "Welcome Admin";
} else {
    echo "Not logged in";
}

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications