Check if PHP session has already started

后端 未结 26 2081
醉酒成梦
醉酒成梦 2020-11-22 03:08

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 s

26条回答
  •  情深已故
    2020-11-22 03:39

    PHP_VERSION_ID is available as of PHP 5.2.7, so check this first and if necessary , create it. session_status is available as of PHP 5.4 , so we have to check this too:

    if (!defined('PHP_VERSION_ID')) {
        $version = explode('.', PHP_VERSION);
        define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
    }else{
        $version = PHP_VERSION_ID;
    }
    if($version < 50400){
        if(session_id() == '') {
            session_start();
        }
    }else{
        if (session_status() !== PHP_SESSION_ACTIVE) {
            session_start();
        }
    }
    

提交回复
热议问题