OpenCart PHP Session /tmp Files Filling Up

Posted by Paul on May 8, 2017

When users come to a website, especially an ecommerce website, it's useful for the server to be able to keep track of who they are so it can do things like display their shopping cart. To do this, the server needs to keep a little file on the server (as well as one in the browser called a cookie).

If you're lucky enough to have a lot of users, each of them will be creating at least one session file and it is possible that either the size of these or just the number of them will grow too big and new "sessions" won't be able to be created. In that case, your pages may load, but users won't be able to log in, or add things to their cart, which is obviously very bad news.

Even worse, it can be hard to track down this problem (and indeed hard to even spot) because the error messages are unlikely to explain what is causing the issue.

If this is the problem you're likely to see this sort of error in the logs:

PHP Fatal error: Uncaught exception 'ErrorException' with message 'Error: Can't create/write to file '/tmp/#sql_676_0.MYD' (Errcode: 28)

This is likely to happen if for some reason, your server isn't clearing your /tmp (temporary) files fast enough. Many servers have a space limit and some have a limit on the actual number of files you can store, which is called the inode limit because on Unix/Linux systems, each file has a number called its inode.

The process of deleting old/unnecessary data is called Garbage Collection, and in PHP you can configure how this is done in the php.ini file in the web root.

There are three variables that need to be set in PHP to make sure that Garbage Collection happens correctly and they are:

session.gc_maxlifetime = 21600
session.gc_probability = 1
session.gc_divisor = 100

Set session.gc_maxlifetime to be the number of seconds you want each tmp file to last before it's deleted. If you login to the admin in OpenCart, this is the number of seconds until you will automatically be logged out. For example to set half an hour, you would do 60 seconds times 30 minutes which would be a value of 1800 seconds.

The other two variables are related to when the Garbage Collector will run, and it's important that they are set to the values above if you're having problems with this.

So to fix issues with your /tmp session files filling up and not being cleared, make sure the above 3 lines are added to your php.ini in the web root and also make that they have taken effect by creating a php script and adding this code:

<?php
    phpinfo();
?>

On the output of this script you should search for the 3 settings above and verify that they are indeed the values you've set. If not, then you may need to ask your web host if they have some global settings that are over-riding them.

Did this fix your issue? Or are you still having issues? Let us know in the comments!

blog comments powered by Disqus