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.";
}