session_status

(PHP >= 5.4.0)

session_status返回当前会话状态

说明

int session_status ( void )

session_status() 被用于返回当前会话状态。

返回值

  • PHP_SESSION_DISABLED 会话是被禁用的。
  • PHP_SESSION_NONE 会话是启用的,但不存在当前会话。
  • PHP_SESSION_ACTIVE 会话是启用的,而且存在当前会话。

参见

User Contributed Notes

ayon at hyurl dot com 10-Oct-2016 02:17
This is how the session_status() works:
<?php
function session_status(){
    if(!
extension_loaded('session')){
        return
0;
    }elseif(!
file_exists(session_save_path().'/sess_'.session_id()){
        return
1;
    }else{
        return
2;
    }
}
?>
Ben (aocool at msn d0t com) 16-Feb-2016 05:46
Just another function to determine whether the session has already started:

function is_session_started () {
    return function_exists ( 'session_status' ) ? ( PHP_SESSION_ACTIVE == session_status () ) : ( ! empty ( session_id () ) );
}
sasi dot viragelet at gmail dot co 02-Feb-2015 07:45
Maybe depending on PHP settings, but if return values are not the above, then go for this:
_DISABLED = 0
_NONE = 1
_ACTIVE = 2
Ollea 30-Jan-2014 08:02
If you started and closed a session then test ( session_id() === '' ) to check if a session is active it won't work, session_id() returns an ID even if the session is closed.

Anybody knows another way before PHP 5.4 to check if a session is really not currently active ?
coder dot ua at gmail dot com 15-Oct-2013 11:13
Universal function for checking session status.

<?php
/**
 * @return bool
 */
function is_session_started()
{
    if (
php_sapi_name() !== 'cli' ) {
        if (
version_compare(phpversion(), '5.4.0', '>=') ) {
            return
session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
        } else {
            return
session_id() === '' ? FALSE : TRUE;
        }
    }
    return
FALSE;
}

// Example
if ( is_session_started() === FALSE ) session_start();
?>
php at kenman dot net 03-Aug-2013 12:45
The purpose of this functionality can aid you specifically in cases where code -- prior to your current code -- might have opened a session and then closed it.

Specifically, depending on $_SESSION, session_id(), and the SID constant to determine if a session is active will FAIL if a session has previously been opened & closed within the same request cycle.

Please see the original bug report here:

https://bugs.php.net/bug.php?id=52982
php at pointpro dot nl 16-Apr-2013 04:06
The advice of ive_insomnia at live dot com should be taken with great care.

First of all, while his use case for session_status is valid, a simpler way to avoid the warning is:

<?php
if (!isset($_SESSION)) { session_start(); }
?>

The example of session_status uses the raw values of constants (2 in this case) created specifically for the purpose of not having to use magic numbers.

Better code would be:

<?php
if (session_status() !== PHP_SESSION_ACTIVE) {session_start();}
?>

The same can be done using

<?
if (session_id() === "") { session_start(); }
?>

The use of this function is lies more towards status management: change the behavior of a script when sessions are disabled altogether, for example.