session_status() returns what?

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Fran3
New php-forum User
New php-forum User
Posts: 14
Joined: Sun Oct 08, 2017 4:01 pm

Sat Mar 10, 2018 8:51 am

I'm a little confused about what session_status() returns.

Try this code...

Code: Select all

<?php 
// _DISABLED = 0
// _NONE = 1
// _ACTIVE = 2
echo session_status();
?>
and apparently you get 0, 1, or 2 returned... note three potential values ... a 0 or a 1 or a 2
then I see this example...

Code: Select all

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;
}
Which would return true if session_status were a 1 or a 2... or false if 0... right?
BUT... according to the above session_status = 1 means "none" meaning no session is active... right?

I'm confused.

Thanks for any help.
Fran3
New php-forum User
New php-forum User
Posts: 14
Joined: Sun Oct 08, 2017 4:01 pm

Sat Mar 10, 2018 9:02 am

OK, I dug a little more and here is the best answer...

from: http://www.xpertdeveloper.com/2012/01/s ... s-php-5-4/

Code: Select all

session_start();
  
  $status = session_status();
  
  if($status == PHP_SESSION_DISABLED)
  {
    echo "Session is Disabled";
  }
  else if($status == PHP_SESSION_NONE )
  {
    echo "Session Enabled but No Session values Created";
  }
  else
  {
    echo "Session Enabled and Session values Created";
  }
again from the site...
In PHP 5.4 with this function session_status() we will have three new constants available which provides the different status of the session.

Here are those status constants:

PHP_SESSION_DISABLED : Session is Disabled
PHP_SESSION_NONE : Session is Enabled and No one values created in Session Yet
PHP_SESSION_ACTIVE : Session is Enables and You have one or more values created in Session.
So
when session_status() returns
0 it means... Session is Disabled
1 it means... Session is Enabled and No one values created in Session Yet
2 itmeans... Session is Enables and You have one or more values created in Session.

This more complete explination helped me get it. Maybe it will help others along the way.
Post Reply