How can i check if session id 066efd5a182729d6fdbd43cb3a80adde exists or not? (I know it existed at some point in the past, but I need to know if it still does or not)
As with any other session ID as well.
Ensure you have no session opened, then set the session_id($id)
and start the session session_start()
. You find $_SESSION
populated if there is some data associated with that (new) session id.
Now, PHP has an option to change the session id on start if it does not exists (Cf. _session.use_strict_mode; use_strict_mode in php sessions). So comparing after start if the ID is unchanged will tell you if the session exists.
Then decide what you want to do with the previously (non-) existing session, like kill it with fire, unset the cookie etc. (depends on context.)
Given, you only want to check the session id:
assert(PHP_SESSION_NONE === session_status());
assert(empty(session_id()));
echo session_exists("066efd5a182729d6fdbd43cb3a80adde")
? "this session exists" : "this session has expired";
assert(PHP_SESSION_NONE === session_status());
assert(empty(session_id()));
without affecting HTTP response headers, including not setting session cookie headers, here an example function:
function session_exists(string $session_id): bool {
session_id($session_id);
session_start(['cache_limiter' => '', 'use_cookies' => false, 'use_strict_mode' => true,]);
$exists = $session_id === session_id();
$exists ? session_abort() && session_id('') : session_destroy();
return $exists;
}
Take care, it is with no error handling, e.g. session_start() may return false which means there was a failure.