Web Development

Get Session Status in PHP 5.4

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 [code]session_start()[/code] 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.

[cc lang=”php”]
session_start();

if(isset($_SESSION))
{
echo “Started”;
}
else
{
echo “Not Started”;
}
[/cc]

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 [code]session_status()[/code].

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.

[cc lang=”php”]
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”;
}
[/cc]

In PHP 5.4 with this function [code]session_status()[/code] 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.

Shares:
  • Get Session Status in PHP 5.4 | PHP | Syngu
    January 27, 2012 at 12:30 pm

    […] Up to PHP 5.3 we do not have any proper/core function which checks the status of the session.    PHP Read the original post on DZone… […]

    Reply
  • Dawn
    Dawn
    December 22, 2012 at 5:32 am

    That’s a stupid 5.3 function. An empty $_SESSION array is not indicative of an existing session.

    Reply
  • Guest
    Guest
    December 20, 2019 at 2:53 pm

    That’s a stupid 5.3 function. An empty $_SESSION array is not indicative of an existing session.

    Reply
  • […] feature in PHP. For some points I have already posted the article for example, Traits in PHP 5.4, Session Status in PHP 5.4. For the remaining one I will post the articles later […]

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *