I also come across this, and setting in $_SESSION is not an option for me. For PHP 5.3.8:
- If any session is started with a request,
define('SID') will return FALSE , and $_SESSION will not be set. - This does not depend on whether
session_id() to set the session identifier or not. - After defining the first
session_start() , SID and $_SESSION , an empty array is set. session_destroy() disables session_id() , then this is an empty string. SID will remain defined (and set the previous value for it, which may be an empty string). $_SESSION remains unchanged. It will get reset / populated next time session_start .
With these states, especially as session_id() you can call between them to set the identifier for the next session, it is not possible to safely determine the state of the session using SID , $_SESSION and session_id() .
An βattemptβ using session_start() (for example, with @ ) may not be very useful, as it will change the session status and change the contents of $_SESSION (and add a set-cookie header if the cookie was not part of the request). This was inappropriate in my case.
While I was running the tests, I came across this behavior that you cannot try to change the ini session.serialize_handler setting when the session is active, even if you set it to the same value. The same is true for session.use_trans_sid Docs , which is easier. This led me to the following function:
function session_is_active() { $setting = 'session.use_trans_sid'; $current = ini_get($setting); if (FALSE === $current) { throw new UnexpectedValueException(sprintf('Setting %s does not exists.', $setting)); } $result = @ini_set($setting, $current); return $result !== $current; }
As far as I see, the error only checks the active status of the session (not disconnected), so this should not return a false positive result when disconnecting sessions.
For this function to be compatible with PHP 5.2, it needs a little modification:
function session_is_active() { $setting = 'session.use_trans_sid'; $current = ini_get($setting); if (FALSE === $current) { throw new UnexpectedValueException(sprintf('Setting %s does not exists.', $setting)); } $testate = "mix$current$current"; $old = @ini_set($setting, $testate); $peek = @ini_set($setting, $current); $result = $peek === $current || $peek === FALSE; return $result; }
Some sandbox .
hakre Oct 05 2018-11-11T00: 00Z
source share