Get Session Status in PHP 5.4

By Avinash

January 24, 2012Web Development3 Comments

Up to PHP 5.3 we do not have any proper/core function which checks the status of the session.

Here status means, We have called the session_start() function but we wants to make sure that really session is started or not. So this status check function is not available upto PHP 5.3.

So up to PHP 5.3 if we want to check the status of the Session then we have to use below code.

Check Session Status in PHP 5.3

Right now we can use below block code to check if session is started or not.

session_start(); if(isset($_SESSION)) { echo "Started"; } else { echo "Not Started"; }
  session_start();
 
  if(isset($_SESSION))
  {
    echo "Started";
  }
  else
  {
    echo "Not Started";
  }

Check Session Status in PHP 5.4

In PHP 5.4 we have native function which provide us the status of the session either it is started or not. This function is session_status().

This function does not take any parameter and it will return the integer based on the session status.

Have a look at the below code block which shows the use of this function.

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"; }
  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";
  }

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:

  1. PHP_SESSION_DISABLED : Session is Disabled
  2. PHP_SESSION_NONE : Session is Enabled and No one values created in Session Yet
  3. PHP_SESSION_ACTIVE : Session is Enables and You have one or more values created in Session.

Conclusion

From PHP 5.4 we will have a native function which is session_status() and also we will have 3 status for the same.

Share This Article