session_gc
session_gc
Perform session data garbage collection
**session_gc**
By default, PHP uses to run the session garbage collector probabilistically on each request. There are some limitations with this approach: session.gc_probability
For production systems, it is recommended to disable the
probability-based garbage collection by setting
to
and explicitly trigger the garbage collector periodically, for example by using "cron" on
UNIX-like systems to run a script that calls .
session.gc_probability0``session_gc
Opmerking: > When calling from a command-line php script, the must be set to the same value as web requests, and the script must have access and delete permissions for the session files. This may be affected by the user the script runs as, and container or sandboxing features such as systemd's option.
session_gcsession.save_pathPrivateTmp=
returns the number of deleted session
entries on success, return.falseforfailure.
session_gc
Opmerking: > Old session save handlers do not return the number of deleted session entries, but rather only a success/failure flag. If this is the case, is returned regardless of how many session entries are actually deleted.
1
Voorbeeld: example for task managers like cron
<?php
// Note: This script should be executed by the same user of web server process.
// Need active session to initialize session data storage access.
session_start();
// Executes GC immediately
session_gc();
// Clean up session ID created by session_start()
session_destroy();
?>
Voorbeeld: example for user accessible script
<?php
// Note: session_gc() is recommended to be used by a task manager script, but
// it may be used as follows.
// Used for last GC time check
$gc_time = '/tmp/php_session_last_gc';
$gc_period = 1800;
session_start();
// Execute GC only when GC period elapsed.
// i.e. Calling session_gc() every request is waste of resources.
if (file_exists($gc_time)) {
if (filemtime($gc_time) < time() - $gc_period) {
session_gc();
touch($gc_time);
}
} else {
touch($gc_time);
}
?>
session_start``session_destroysession.gc_probability