note 72579 added to function.session-start
| From: | r dot s dot goldsmith at far-blue dot co dot uk | Date: | Wed, 24 Jan 2007 23:22:41 +0000 |
| Subject: | note 72579 added to function.session-start | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-122167@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 85.116.0.130
Probable Submitter: 84.92.17.57
----
Manual Page -- http://www.php.net/manual/en/function.session-start.php
Edit -- https://master.php.net/note/edit/72579
Del: integrated -- https://master.php.net/note/delete/72579/integrated
Del: useless -- https://master.php.net/note/delete/72579/useless
Del: bad code -- https://master.php.net/note/delete/72579/bad+code
Del: spam -- https://master.php.net/note/delete/72579/spam
Del: non-english -- https://master.php.net/note/delete/72579/non-english
Del: in docs -- https://master.php.net/note/delete/72579/in+docs
Del: other reasons-- https://master.php.net/note/delete/72579
Reject -- https://master.php.net/note/reject/72579
Search -- https://master.php.net/manage/user-notes.php