asked in PHP by (8.8k points)
0 like 0 dislike
31 views

I have a PHP file that is sometimes called from a page that has started a session and sometimes from a page that doesn't have session started. Therefore when I have start session (run function session_start()) on this script I sometimes get the error message for "session already started". Is there a way to check if session has already started? How I can to check if PHP session has already started?


check if php session has already started
php if session started

1 Answer

0 like 0 dislike
answered by (8.8k points) edited by

Recommended way for versions of PHP >= 5.4.0, PHP 7:

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

Reference: http://www.php.net/manual/en/function.session-status.php

For versions of PHP < 5.4.0:

if(session_id() == '') {
    session_start();
}

For versions of PHP prior to PHP 5.4.0:

if(session_id() == '') {
    // session isn't started
}

if (session_status() != PHP_SESSION_ACTIVE) {
	session_start();
}

if (!isset($_SESSION)) session_start();

Generic solution:

if (version_compare(phpversion(), '5.4.0', '<')) {
     if(session_id() == '') {
        session_start();
     }
 }
 else
 {
    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }
 }

Check:

session_id(); // returns empty string
session_start();
session_id(); // returns session hash
session_destroy();
session_id(); // returns empty string, ok, but then
session_id('foo'); // tell php the session id to use
session_id(); // returns 'foo', but no session is active.

For all php version

if ((function_exists('session_status') && session_status() !== PHP_SESSION_ACTIVE) || !session_id()) {
  session_start();
}

Check this :

<?php
/**
* @return bool
*/
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();
?>

if (session_id() === "") { session_start(); }

This should work for all PHP versions. It determines the PHP version, then checks to see if the session is started based on the PHP version. Then if the session is not started it starts it.

function start_session() {
  if(version_compare(phpversion(), "5.4.0") != -1){
    if (session_status() == PHP_SESSION_NONE) {
      session_start();
    }
  } else {
    if(session_id() == '') {
      session_start();
    }
  }
}

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

Related questions

0 like 0 dislike
1 answer 1 view
0 like 0 dislike
1 answer 29 views
0 like 0 dislike
1 answer 51 views
0 like 0 dislike
1 answer 41 views
0 like 0 dislike
1 answer 17 views
0 like 0 dislike
1 answer 46 views
0 like 0 dislike
1 answer 15 views
...