note 72579 deleted from function.session-start by peehaa
| From: | peehaa@php.net | Date: | Fri, 09 Dec 2016 22:07:42 +0000 |
| Subject: | note 72579 deleted from function.session-start by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208327@lists.php.net to get a copy of this message | ||
Note Submitter: r dot s dot goldsmith at far-blue dot co dot uk
----
If you ever want to know whether a session is open or has already been closed (e.g. with
session_write_close()) when you need to open one (in my case, writing a framework and, therefore,
not controlling the session directly), I have been unable to find an official check. However, I did
notice that session_start() has the following behaviour:
* If the session has been open before but is now closed, when you call session_start() the $_SESSION
array is reset to the content of the session.
* If the session is already open, the $_SESSION array is left totally untouched.
Therefore, I came up with the following (obviously assuming you have already tested $_SESSION
exists):
<?php
$_SESSION['testMe']='test';
@session_start();
if(isset($_SESSION['testMe']))
{
echo "Session was open\n";
unset($_SESSION['testMe']);
}
else
echo "Session was closed\n";
?>
Until someone can tell me the 'official' way to do it, this seems to work.
Just to save some confusion, $_SESSION, session_id() and the SID define all maintain their values
after a session_write_close() so are of no use in identifying if a session is currently open or not.
Hence this trick.