7

Is is possible to have two user data to have different timeout/expiry time? Let say first data "param_1" expired in 1 day and "param_2" expired in a month. How to do that with CI session library. Something that might be like this

$this->session->set_userdata('param_1', 86400);  // seconds in a day
$this->session->set_userdata('param_2', 2592000); // seconds in a month
Va1iant
  • 203
  • 3
  • 12
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – Tpojka Oct 31 '17 at 07:40
  • Maybe this will help you https://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes/1270960#1270960 – Anand Pandey Nov 15 '17 at 11:12

2 Answers2

7

This is possible via "Tempdata":

$this->session->tempdata($key, $value, $validForTime);

However, a single session should not last more than a few hours to maybe a day. If you want some variable to persist for longer than that, sessions are absolutely the wrong tool to use for that.

Narf
  • 14,600
  • 3
  • 37
  • 66
  • my objective is to know if a user has been logged out because of his/her login session expired, or just because the person purposely logout or probably never login before. So i have an idea to create one very long-lived session variable, separate from login session to differentiate the case. From there an appropriate message will be displayed to user. Any idea how to do this? As for Tempdata that seems promising, but it seemed it's only suitable for short-term session, not sure the maximum value it can hold – Va1iant Nov 13 '17 at 12:55
  • 1
    That's deffinately a [XY problem](https://meta.stackexchange.com/a/66378/204788). The idea that you have is not even feasible. – Narf Nov 13 '17 at 13:51
  • Very simple just add store another value in session upon manual logout – Sahib Khan Nov 18 '17 at 06:09
  • @SahibKhan your answer is valid when user did manual logout, but more times the user gets logged out automatically when their session expired. But you open another possible solution for my problem, if one can determine when codeigniter reject the cookies due to timeout, then we can put code to set for auto-logout event – Va1iant Nov 20 '17 at 12:58
  • You are now asking for time out hook. I would say, determine if the user is logged out and the manual logged out key is there (this is timed out logout do something and then remove the manual logged out key) – Sahib Khan Nov 21 '17 at 20:52
  • How to add hook is explained in this thread. https://forum.codeigniter.com/thread-54559.html – Sahib Khan Nov 21 '17 at 20:54
5

This would only be possible if you set a cookie on the client on a different domain than the normal one with a longer expiration date and verifying that instead of session data. In more practical terms, using an ajax .get on the page to a specific url that verifies both cookies without starting the session (since the session can already be expired).

DevionNL
  • 142
  • 1
  • 12