Detect if PHP session exists

PhpSession

Php Problem Overview


Facebook now offer subscriptions to users so you can get realtime updates on changes. If my app receives an update, I plan to store it in the database. I would also like to detect if their session exists. If it does then I could update the data in there too.

My session IDs are MD5(fb_id + secret) so I could easily edit their session. The question is how can I detect if the session exists.

Php Solutions


Solution 1 - Php

I use a combined version:

if(session_id() == '' || !isset($_SESSION) || session_status() === PHP_SESSION_NONE) {
    // session isn't started
    session_start();
}

Solution 2 - Php

If $_SESSION (or $HTTP_SESSION_VARS for PHP 4.0.6 or less) is used, use isset() to check a variable is registered in $_SESSION.

isset($_SESSION['varname'])

Solution 3 - Php

If you are on php 5.4+, it is cleaner to use session_status():

if (session_status() == PHP_SESSION_ACTIVE) {
  echo 'Session is active';
}
  • PHP_SESSION_DISABLED if sessions are disabled.
  • PHP_SESSION_NONE if sessions are enabled, but none exists.
  • PHP_SESSION_ACTIVE if sessions are enabled, and one exists.

Solution 4 - Php

Which method is used to check if SESSION exists or not? Answer:

isset($_SESSION['variable_name'])

Example:

isset($_SESSION['id'])

Solution 5 - Php

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();

Solution 6 - Php

The original code is from Sabry Suleiman.

Made it a bit prettier:

function is_session_started() {

	if ( php_sapi_name() === 'cli' )
		return false;

	return version_compare( phpversion(), '5.4.0', '>=' )
		? session_status() === PHP_SESSION_ACTIVE
		: session_id() !== '';
}

First condition checks the Server API in use. If Command Line Interface is used, the function returns false.

Then we return the boolean result depending on the PHP version in use.

In ancient history you simply needed to check session_id(). If it's an empty string, then session is not started. Otherwise it is.

Since 5.4 to at least the current 8.0 the norm is to check session_status(). If it's not PHP_SESSION_ACTIVE, then either the session isn't started yet (PHP_SESSION_NONE) or sessions are not available altogether (PHP_SESSION_DISABLED).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPabloView Question on Stackoverflow
Solution 1 - PhpReign.85View Answer on Stackoverflow
Solution 2 - PhpSebastián GrignoliView Answer on Stackoverflow
Solution 3 - PhpTotoView Answer on Stackoverflow
Solution 4 - PhpRushabh KoradiaView Answer on Stackoverflow
Solution 5 - PhpSabry SuleimanView Answer on Stackoverflow
Solution 6 - Phps3cView Answer on Stackoverflow