views:

69

answers:

2

How or when garbage collection with session will take place in case of session managing with database in php? I done session management with database. But it is not deleting the user details who completed the time of expiration.

A: 

You must register gc handler as part of call to session_set_save_handler() function, that will take care of removing expired sessions.

Also, make sure that ini settings session.gc_probability, session.gc_divisor and session.gc_maxlifetime are configured to suit your needs for garbage collection.

mr.b
Yea, I already done it. session_set_save_handler( array( Is it enough?
shinod
See updated post. Yes, that should be enough. Keep in mind however that garbage collection will by default occur only once in 100 requests, if you leave those ini settings at default. Increase gc_probability (or decrease gc_divisor) if you wish to run gc handler more frequently.
mr.b
+2  A: 

it is not deleting the user details who completed the time of expiration.

People always seem to get confused by this.

Garbage collection is triggered based on a throw of the dice whenever there is a call to session_start(). So if all your customers suddenly stopped accessing your webserver at the same time, garbage collection would never kick in and the session data would persist indefinitely.

However it is the responsibility of the session handler to only return session data if the session was previously accessed before the TTL had expired. Therefore every time the session is saved, the handler must update the timestamp on the session data file/record even if the data has not changed.

If session data is being returned by the handler after the TTL has expired then there is a bug in the session handler.

However you merely state that the data exists after the session has expired - this is perfectly normal.

OTOH if you simply want to reduce the overhead of storing a lot of expired sessions then you can force garbage collection to occur more frequently by increasing the gc_probability or decreasing the gc_divisor. But then you're pushing more processing effort into the request handler.

C.

symcbean