The controlling of the session time

setcookie("cookieName", "cookie", time()+5, "");

The cookie will be disappeared after 5 seconds by the code above.
Is there any way to set a session which will be disappeared 5 seconds later like the above?

Do you have any suggestion to control the keeping time of a session?

In your php.ini there should be a setting called “session.gc_maxlifetime” the number of seconds that is set for determines how long a session lasts for after that time the session gets flagged as garbage.

A combination of “session.gc_probability” and “session.gc_divisor” determine the % chance of the “garbage collection” running which deletes expired sessions.

Are you using the file system of a database table to store the sessions in?

Do I need it for making a stable webSite?

I guess that the file system of a database table is like the following

sessionTable sessionName / setTime / value

Is the sessionTable okay or does it need more columns?

In my experience, you’re best to store a timestamp in the session itself and to use that to determine whether to expire the session via your own code, rather than rely on PHP’s session expiry. I’ve found that it doesn’t always behave quite as you might expect.

You may want a session to expire a certain time after it was created, or you may want it to be after a specific amount of inactivity. You can pick and choose by just deciding when to update your timestamp. You can also more easily set different session lengths for different users if you like, too.

It requires that you code the solution into your site, rather than use the built in session expiry, but it’s not a lot of work and it’s worth doing IMO

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.