Detect if PHP session exists



PHP Snippet 1:

isset($_SESSION['varname'])

PHP Snippet 2:

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

PHP Snippet 3:

if (session_status() == PHP_SESSION_ACTIVE) {
  echo 'Session is active';
}

PHP Snippet 4:

isset($_SESSION['variable_name'])

PHP Snippet 5:

isset($_SESSION['id'])

PHP Snippet 6:

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