PHP 8.6.0 Alpha 2 available for testing

session_gc

(PHP 7 >= 7.1.0, PHP 8)

session_gc执行会话数据垃圾回收

说明

function session_gc(): int|false

默认情况下,PHP 使用 session.gc_probability 在每次请求时以概率方式运行会话垃圾回收器。这种方法存在一些局限性:

  • 低流量站点的会话数据可能无法在预期时间内被删除。
  • 高流量站点的垃圾回收器可能运行过于频繁,执行不必要的额外工作。
  • 垃圾回收在用户请求时执行,用户可能会感受到延迟。

对于生产系统,建议通过将 session.gc_probability 设置为 0 来禁用基于概率的垃圾回收,并定期显式触发垃圾回收器,例如在类 UNIX 系统上使用 "cron" 运行调用 session_gc() 的脚本。

注意: 从命令行 PHP 脚本调用 session_gc() 时, session.save_path 必须设置为与 Web 请求相同的值,并且脚本必须对会话文件具有访问和删除权限。这可能受到脚本运行用户的影响, 以及容器或沙箱功能(如 systemd 的 PrivateTmp= 选项)的影响。

参数

此函数没有参数。

返回值

session_gc() 成功时返回删除的会话条目数, 或者在失败时返回 false

注意: 旧的会话保存处理程序不返回删除的会话条目数,而只返回成功/失败标志。在这种情况下, 无论实际删除了多少会话条目,都会返回 1

示例

示例 #1 用于 cron 等任务管理器的 session_gc() 示例

<?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();
?>

示例 #2 用于用户可访问脚本的 session_gc() 示例

<?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);
}
?>

参见

添加备注

用户贡献的备注 2 notes

up
1
i dot carvallo at gmail dot com
1 year ago
Do not use:

if (session_gc() == false)
OR
if (!session_gc())

to evaluate if the garbage collector was triggered successfully since it also returns how many sessions it deleted, which can be 0 (and that evaluates to false with loose operators).

Sounds dumb, but it's a pitfall i fell into and it generated some confusion. Instead, use strict operator "===":

if (session_gc() === false)
up
0
ridaelkouri at gmail dot com
1 year ago
The session.gc() function does not seem to work alone. it deletes the data on the server but the data remains on the browser in the form of the cookie. the following code deletes the session files on the server but not on the browser. 

ini_set('session.gc_maxlifetime', 10);
ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 1);

// Start the session
session_start();
$_SESSION['test'] = 'temporary data';

session_write_close();

// Wait for 15 seconds to ensure the session expires
sleep(15);

// Manually start the session again to trigger session handling
session_start();

session_gc();

// Check if the session data is still available
if (isset($_SESSION['test'])) {
    echo "Session is still active.";
} else {
    echo "Session expired and file deleted.";
}

but this code delete the session files on the server and also deletes the cookie as well as making the super global empty:

session_start();
$_SESSION['test'] = 'temporary data';

session_write_close();

// Wait for 15 seconds to ensure the session expires
sleep(15);

session_start();

// Manually trigger garbage collection
setcookie(session_name(), '', time() - 10);
$_SESSION = [];
session_gc();

// Check if the session data is still available
if (isset($_SESSION['test'])) {
    echo "Session is still active.";
} else {
    echo "Session expired and file deleted.";
}
To Top