|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-10-04 04:45 UTC] 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() Patchesphp-trunk-session-status-bools (last revision 2011-07-26 18:53 UTC by arpad@php.net)php-trunk-session-status (last revision 2011-07-26 18:52 UTC by arpad@php.net) session.c.patch.txt (last revision 2010-10-04 07:05 UTC by php dot net at kenman dot net) Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 08:00:01 2025 UTC |
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.