Are there limits for session variables?

As @Thariama said, there’s no limit on the number of variables; also, there’s no limit on the amount of data you can store in a session (I’ve seen sessions tens of MB in size).

As the size of a session gets larger, you’ll run into various quirks though: PHP 5 deserializes the whole session into memory at session_start() (using the default session handler – you can make you own solution, of course); with a 20 MB session and 50 concurrent users, your scripts start to be severely limited by disk access speeds (a.k.a. “script startup is slow as molasses” – the sessions alone would be hogging a GB of RAM); in the end, we dedicated a box to keep as many sessions as possible in its RAM, and the frontend boxes accessed them over NFS (although it helped in our case, this may be overkill for you).

Note that for many concurrent users and session storage on disk, the number of session temporary files may cause problems with filesystem limits (e.g. how many files can be in one directory before you run into problems with stat() performance), or other limits (we once found the hard way that a box was configured to only allow 4096 open files at the same time). None of this is really session-specific, but can be triggered by session handling.

Leave a Comment