note 129834 added to function.session-gc
| From: | ridaelkouri@gmail.com | Date: | Mon, 04 Nov 2024 13:29:50 +0000 |
| Subject: | note 129834 added to function.session-gc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221629@lists.php.net to get a copy of this message | ||
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.";
}
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 88.156.137.65)
----
Manual Page -- https://php.net/manual/en/function.session-gc.php
Edit -- https://main.php.net/note/edit/129834
Del: integrated -- https://main.php.net/note/delete/129834/integrated
Del: useless -- https://main.php.net/note/delete/129834/useless
Del: bad code -- https://main.php.net/note/delete/129834/bad+code
Del: spam -- https://main.php.net/note/delete/129834/spam
Del: non-english -- https://main.php.net/note/delete/129834/non-english
Del: in docs -- https://main.php.net/note/delete/129834/in+docs
Del: other reasons-- https://main.php.net/note/delete/129834
Reject -- https://main.php.net/note/reject/129834
Search -- https://main.php.net/manage/user-notes.php