In my custom Wordpress theme I want to keep some user's data stored over time (let's say 5 days) using $_SESSION variable.

At a certain point in my theme I do a very simple thing:

$_SESSION['my_data'] = $my_object;

session_start() may be useless because it should have been called by Wordpress core before my script, but that's not the point. The problem is that my custom data gets erased after few hours, even if I change it to:

$_SESSION['my_data'] = $my_object;

I'm wondering how the $_SESSION variable is managed by the Wordpress code and if there is a way to extend its validity... or better to increase the validity by 5 days to just my data, that is always stored in $_SESSION['my_data']

  • 1
    Session cookies are destroyed when the browser is closed. Why do you expect yours to behave differently? – fuxia Jan 29 '17 at 22:55
  • Session cookies are destroyed when the browser is closed, really? Because if I simply close everything in the client and reopen the page after, say, one hour, the $_SESSION is still as I left it. "Invalidation" happens if I wait more hours. Anyway, as you see I don't have such great knowledge about PHP sessions... should I use instead cookies? – TheUnexpected Jan 30 '17 at 19:45

One tip from here:

Set cookie parameters defined in the php.ini file. The effect of this function only lasts for the duration of the script. Thus, you need to call session_set_cookie_params() for every request and before session_start() is called.

You are calling it after.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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