I wanted to save a huge array in a $_SESSION variable. If I understand it correctly, Drupal saves the entire $_SESSION into the sessions table. But when the $_SESSION variable gets to around 3mb, MySQL throws an error.

I don't want to increase max_allowed_packet size, and I don't really need Drupal to save the array variable in the sessions table.

So, is there a way to set a $_SESSION variable that can be excluded from being saved into the sessions table?


1 Answer 1


You should probably look first at either not storing this data but instead deferring its calculation, or at alternative storage methods, eg perhaps cache_set() and cache_get(). You could easily run into the same DB limitations there of course.

You probably don't need to store this data to the session at all, and as you've discovered it can be problematic to do so.

I've seen developers use $_SESSION as a global variable to pass data around, and as temporary per-visitor storage. This is possible, but there are limitations and likely better ways - for which you might need to zoom out to the actual problem at hand. (Do you need to store a rendered collection of kitten profiles, or just a list of kitten IDs then render later?)

If you do actually need to store such large datasets to the session, you'd probably have to replace the default session management in Drupal or reconfigure your DB (which you've said you'd rather not do). In that case perhaps modules like Session Proxy or perhaps Memcache would help ... But try not to go there :)

  • Thank you. I am actually collecting the data through a batch process - and write to an excel file. It's just that through my tests, it is faster to collect all the data first, then do a single write to the excel file. Instead of writing to the excel file repeatedly.
    – Bibokid
    May 29, 2017 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.