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:
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:
System -> Configuration -> Checkout -> Shopping Cart | |
Quote Lifetime (days) -> 1 |
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.
Magento remove session id from URL
Magento displays session ID in url, something like:
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),
protected $_useSessionInUrl = true; |
Change that value to “false”. That should now prevent session IDs appearing in URL.
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.
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
$session = Mage::getSingleton('core/session', array('name' => 'frontend')); | |
echo $session->getMyCustomData(); |
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.
$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.
$adminsession = Mage::getSingleton('admin/session', array('name'=>'adminhtml')); | |
if($adminsession->isLoggedIn()) { | |
echo "Welcome Admin"; | |
} else { | |
echo "Not logged in"; | |
} |
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)