A
A
Alexey Maksimov2015-11-04 16:26:48
PHP
Alexey Maksimov, 2015-11-04 16:26:48

Is the session life extended with each session_start()?

There is a site on which the session is constantly twitching by calling session_start () to get any data. In php.ini session life is set to 1 hour. I'm interested in how the garbage collector works. Does it count the time from session creation or from its last use?
Because after an hour of creation, the garbage collector deletes all sessions, even if they are constantly called. I don’t understand what the problem is, in the code or in the work of the collector.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2015-11-05
@additivex

Generally yes, it is extended.
Perhaps someone else's garbage collector removes them, as in this story .
tl;dr: The value of session.gc_maxlifetime can be overridden at runtime. And the garbage collector removes all expired sessions from session.save_path. If several different applications use the same session.save_path directory, special effects can be observed.

D
DezzmonD, 2015-11-05
@DezzmonD

1) When the session starts, the $_SESSION variable is created, and after that there is a chance that the garbage collector will start, which will delete all files along the session.save_path path that have "updated_time" < (cuttent_time - "session.gc_maxlifetime"). IMPORTANT: the lifetime of the session ID cookie is not updated!!!
The garbage collector is run with a chance of "session.gc_probability" / "session.gc_divisor" (by default it's usually: 1 / 1000 = 0.001%) on every session start;
2) Yes, when a session starts, PHP takes the ID from the cookie, looks for the session file in ession.save_path with that ID, and updates it with "updated_time".
Solution to your problem:
1) Check how much "session.cookie_lifetime" cookie lifetime costs you.
You could refresh the page as much as you wanted, but the lifetime of the cookie simply passed. If so, then put 0 (before closing the browser) - this is if you need to update the time after each session start. Then the session will live session.gc_maxlifetime
2) If this is not the first option, then it means different scripts use 1 session.save_path folder to store session files. Change the session.save_path folder with ini_set() or contact your host.
PS I also recommend using session_name(). This will help avoid errors if you want to store multiple sessions for different purposes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question