Req #52982 [Asn]: Expose php_session_active to userland via new function (patch included)
| From: | arpad@php.net | Date: | Mon, 25 Jul 2011 22:37:37 +0000 |
| Subject: | Req #52982 [Asn]: Expose php_session_active to userland via new function (patch included) | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-162169@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=52982&edit=1
ID: 52982
Updated by: arpad@php.net
Reported by: php dot net at kenman dot net
Summary: Expose php_session_active to userland via new
function (patch included)
Status: Assigned
Type: Feature/Change Request
Package: Session related
PHP Version: 5.3.3
Assigned To: kalle
Block user comment: N
Private report: N
New Comment:
If you're trying to establish whether it's safe to start your own session, this patch is
deficient because PS(session_status) is not a boolean - it could also be php_session_disabled.
I'd imagine robust code to handle this (rather unusual) case where you require a session but
don't know if it's present, would be something like:
if (session_is_available()) {
if (!session_is_active()) {
if (headers_sent()) {
// bail out
} else {
session_start();
}
}
} else {
// bail out
}
We could instead provide a session_status() function which corresponds to the internal enum
(returning SESSION_DISABLED, SESSION_NONE or SESSION_ACTIVE.)
I assume this is what Kalle was talking about WRT exposing the other values.
Both options are trivial patches. The former seems friendlier but more prone to error.
Previous Comments:
------------------------------------------------------------------------
[2011-07-25 17:25:54] php dot net at kenman dot net
Any update on this?
Simple request; I'm not exactly sure what all Kalle was speaking to WRT "expose
the other values", but what I'm requested here is a tiny piece of functionality,
and that is *all* that is being requested.
------------------------------------------------------------------------
[2010-10-05 07:34:11] php dot net at kenman dot net
pajoye,
I'm not sure if you understand which problem I'm trying to address here; it's
not whether or not session_start() was successful, it's whether or not there is
a current active session.
Many times, in frameworks or other large-scale applications, you have no way of
knowing whether session_start() has already been called. You cannot rely on
session_id(), SID, session_start(), $_SESSION, or any other session functions or
session functionality (that I'm aware of) to tell me "yes, you already have a
session open" or "no, you do not have a session open" _before_ I call
session_start().
------------------------------------------------------------------------
[2010-10-04 12:15:56] pajoye@php.net
The notice is documented and can be disabled using the display error settings. To detect whether or
not session_start was successful, you have to test its return value (as of 5.3.0).
It is actually a documentation problem as the examples in the manual does not check the return
values (maybe add one on top of them using 5.3+).
------------------------------------------------------------------------
[2010-10-04 10:31:43] kalle@php.net
If we are going to expose php_session_active, we might aswell expose the other values. I will cook
up a patch and apply it to trunk shortly
------------------------------------------------------------------------
[2010-10-04 04:45:37] php dot net at kenman dot net
Description:
------------
Attempting to start a session after a session has already been started raises an
E_NOTICE, however, there is no sane way to check if a session has already been
started. The only ways that I could devise were using the shut-up op (@) or by
creating wrapper functions around all of the session functions.
This is extreme because there exists in the PHP source, code which can do this; my
patch exposes this code for userland comsumption.
More info:
http://stackoverflow.com/questions/3788369/how-to-tell-if-a-session-is-active
Test script:
---------------
session_start();
// 1000's of lines of code, which may/may not have closed the session
session_start(); // how to tell if a session is active here?
Expected result:
----------------
Should be able to ask PHP whether or not it knows of an active session.
Actual result:
--------------
Notice: A session had already been started - ignoring session_start()
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=52982&edit=1