Magento Session Problems

There are a number of session problems in Magento – just look around the web and you’ll see examples of people having trouble with cookie lifetimes, folder/file permissions and lot’s of other various tweaks and changes. All of these problems have answers, our recent problem however did not have an answer at all.

Our problem was the fact that our clients Magento website was not adding any products to cart, nor allowing you to login to the admin. In fact when we looked at the var/session folder – nothing was being written to it at all!

All symptoms pointed toward the common problems – cookies / folder permissions. This was not out issue.

Our issue revolved around line 44-46 of app/code/core/Mage/Core/Model/Session/Abstract/Varien.php exactly these lines of code:

if (isset($_SESSION)) {
    return $this;
}

These lines of code appear within the start() function – essentially telling Magento not to carry on setting up it’s session variables if there is already a session in place.

We found that within our index.php was an included file that somehow started another session – therefore telling Magento not to bother (not to bother calling session_start()) – this led to the website displaying correctly, but no ability to add products to cart or login to the admin – nor any error message!

To fix this all we had to do was to add the following line to index.php below where we were including our custom file and above where Magento was being called.

require_once('some/dodgy/session/starting/file.php');
unset($_SESSION);

That was it, that’s all we had to fix. What a headache – and a reminder not to hack your Magento website in too many ways as debugging an issue like this takes hours.