How to tell if a session is active?

i-mad

New Member
Per request, there are a few different ways that you can tell whether or not a session has been started, such as:\[code\]$isSessionActive = (session_id() != "");\[/code\]Or:\[code\]$isSessionActive = defined('SID');\[/code\]However, these both fail if you start a session, then close it; \[code\]session_id()\[/code\] will return the prior session's ID, while \[code\]SID\[/code\] will be defined. Likewise, calling \[code\]session_start()\[/code\] at this point will generate an \[code\]E_NOTICE\[/code\] if you already have a session active. Is there a sane way to check if a session is currently active, without having to resort to output buffering, the shut-up operator (\[code\]@session_start()\[/code\]), or something else equally as hacky?EDIT: I wrote a patch to try to get this functionality included in PHP: http://bugs.php.net/bug.php?id=52982EDIT 8/29/2011: New function added to PHP trunk/5.4 to fix this: "Expose session status via new function, session_status" \[code\]// as of 8/29/2011$isSessionActive = (session_status() == PHP_SESSION_ACTIVE);\[/code\]EDIT 12/5/11: session_status() on the PHP manual.
 
Back
Top