PHP Unconference Europe 2015

session_status

(PHP >=5.4.0)

session_statusDétermine le statut de la session courante

Description

int session_status ( void )

session_status() est utilisée pour connaitre l'état de la session courante.

Valeurs de retour

  • PHP_SESSION_DISABLED si les sessions sont désactivées.
  • PHP_SESSION_NONE si les sessions sont activées, mais qu'aucune n'existe.
  • PHP_SESSION_ACTIVE si les sessions sont activées, et qu'une existe.

Voir aussi

  • session_start() - Démarre une nouvelle session ou reprend une session existante
add a note add a note

User Contributed Notes 5 notes

up
17
coder dot ua at gmail dot com
1 year ago
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();
?>
up
6
php at pointpro dot nl
1 year ago
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.
up
2
php at kenman dot net
1 year ago
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
up
0
dingus_76 at hotmail dot com
1 day ago
A Basic backport of this to php 5.3 can be done with.

<?php
if (!defined('PHP_SESSION_DISABLED')) {
   
define('PHP_SESSION_DISABLED', 0);
}
if (!
defined('PHP_SESSION_NONE')) {
   
define('PHP_SESSION_NONE', 1);
}
if (!
defined('PHP_SESSION_ACTIVE')) {
   
define('PHP_SESSION_ACTIVE', 2);
}
if (!
function_exists('session_status')) {
    function
session_status(){
        if(!
function_exists('session_id')) return PHP_SESSION_DISABLED;
        if (
session_id() === "") return PHP_SESSION_NONE ;
        return
PHP_SESSION_ACTIVE;
    }
}
?>
up
0
Ollea
9 months ago
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 ?
To Top