PHP Function Session_GC
The php function session_gc is used to perform session data GC(garbage collection). PHP does probability based session GC by default. You can hook into this GC by writing your own session handler and applying it with session_set_save_handler(). The function you provide as gc_function will be called when it is time to do GC on sessions. The function has access to the serialized session data that is stored in files on disk or in memory.
The session_gc function is important because it prevents a user from accidentally reconnecting to an old session. This can happen if the session ID is encoded in the URL or if a user has bookmarked your site and tries to access an old session by typing in the session id.
This function creates a temporary file on the server containing the session data. It names the file with a unique 32 digit hexadecimal value prefixed with sess_. The file gets deleted when the session ends. PHP accesses this hexadecimal string from the cookie to get the session variables values.
Probability based GC works quite well but it does have a few problems. 1) Low traffic sites' session data may not be deleted within the preferred duration. 2) High traffic sites may experience too frequent GC. This GC is performed on each request which may slow down the site and the user. It is therefore recommended to execute GC periodically for production systems using, e.g., "cron" for UNIX-like systems. If you do this then make sure to disable probability based GC by setting session.gc_probability to 0.