3

I am using PHP 5.5.38 on apache server 2.4.16. I get this problem of losing the session after sometime (around 25 mins). I found these parameters in php.ini file

session.gc_maxlifetime = 1440
session.gc_probability = 10

But in the code these has been setting up to different values. The code looks like this

ini_set('session.gc_maxlifetime', 60*60*24*7);
ini_set('session.use_cookies', 1);
ini_set('session.gc_probability', 0);

and i confirmed them returning the older values which means its not failing or something.

I don't know where else to look to find the cause of losing the session. Any idea or suggestion would be greatly appreciated.

2
  • Did you check your htaccess file? do you have anything like php_value session.gc_maxlifetime Commented Sep 13, 2017 at 13:24
  • Yes i have also checked htaccess file, nothing there.
    – Shaonline
    Commented Sep 13, 2017 at 13:30

1 Answer 1

1

When you set a config value with ini_set(), it only lasts during the current script execution:

The configuration option will keep this new value during the script's execution, and will be restored at the script's ending.

And, when there are multiple values for session.gc_maxlifetime, the garbage collector will use the lowest:

If different scripts have different values of session.gc_maxlifetime but share the same place for storing the session data then the script with the minimum value will be cleaning the data.

So what you need to do is either have the ini_set() assignments in every page where you use sessions, or, the better option, modify directly the php.ini file to the values you need.

Notice that 25 minutes is aproximately 1440 seconds (24 minutes), so it's definitely using the php.ini's value.

5
  • the ini_set() assignments are used on every page actually. Shouldn't this keep this new value if it is on every page?
    – Shaonline
    Commented Sep 13, 2017 at 13:36
  • 1
    @Shaonline If you have a debian based system, the garbage collection is done with a cronjob that fires every minute or so. This script (and similar utility scripts) will run with the default php.ini values. ishegg is correct, you should set this setting in the main php.ini file, NOT a script in user land. Commented Sep 13, 2017 at 13:37
  • A (bit) "hacky" workaround of what @OptimusCrime said, is setting the session.save_path to another directory so the cronjob can't get to it, with session_save_path(). This is of course less than ideal. Is it at all possible for you to change the values directly in the ini file?
    – ishegg
    Commented Sep 13, 2017 at 13:58
  • Looks like it is the only way to go, but I still haven't figured out the reason. I am saying this because the same code is in two different environments. I am getting this session problem on one only, where php.ini values for session.gc_maxlifetime is 1440 on both environments.
    – Shaonline
    Commented Sep 13, 2017 at 14:09
  • Well, that might be a starting point for your debugging process. What's different between the two? Hardware, software, settings, etc? (obviously, relevant to sessions).
    – ishegg
    Commented Sep 13, 2017 at 14:10

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.