Check if PHP session has already started

后端 未结 26 2119
醉酒成梦
醉酒成梦 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:52

    if (version_compare(PHP_VERSION, "5.4.0") >= 0) {
        $sess = session_status();
        if ($sess == PHP_SESSION_NONE) {
            session_start();
        }
    } else {
        if (!$_SESSION) {
            session_start();
        }
    }
    

    Actually, it is now too late to explain it here anyway as its been solved. This was a .inc file of one of my projects where you configure a menu for a restaurant by selecting a dish and remove/add or change the order. The server I was working at did not had the actual version so I made it more flexible. It's up to the authors wish to use and try it out.

提交回复
热议问题