我有一个PHP文件,有时从一个已经开始会话的页面调用,有时从一个没有会话启动的页面调用。因此,当我在这个脚本上有session_start()时,我有时会得到“会话已经开始”的错误消息。为此,我写了以下几行:

if(!isset($_COOKIE["PHPSESSID"]))
{
  session_start();
}

但这次我收到了警告信息:

注意:未定义变量:_SESSION

是否有更好的方法来检查会话是否已经开始?

如果我使用@session_start,它会使事情正常工作,只是关闭警告?


当前回答

在函数调用之前@会抑制函数调用过程中可能报告的任何错误。

在session_start之前添加@告诉PHP避免打印错误消息。

例如:

在你已经打印了一些东西到浏览器之后使用session_start()会导致一个错误,所以PHP会显示类似“头不能被发送:started at (line 12)”这样的东西,@session_start()在这种情况下仍然会失败,但错误消息不会打印在屏幕上。

在包含文件或重定向到新页面之前,请使用exit()函数,否则将报错。

此代码可以在所有情况下使用:


    <?php 
        if (session_status() !== PHP_SESSION_ACTIVE || session_id() === ""){
            session_start(); 
        }
    ?>

其他回答

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

最后我对状态进行了两次检查。php 5.4 +

if(session_status() !== PHP_SESSION_ACTIVE){session_start();};
if(session_status() !== PHP_SESSION_ACTIVE){die('session start failed');};

基于@Meliza Ramos的回复(见第一个回复)和http://php.net/manual/en/function.phpversion.php,

行动:

如果不存在,定义PHP_VERSION_ID 根据PHP_VERSION_ID定义函数检查版本 定义函数为openSession()安全

只使用openSession()

    // PHP_VERSION_ID is available as of PHP 5.2.7, if our
    // version is lower than that, then emulate it
    if (!defined('PHP_VERSION_ID')) {
        $version = explode('.', PHP_VERSION);

        define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));


        // PHP_VERSION_ID is defined as a number, where the higher the number
        // is, the newer a PHP version is used. It's defined as used in the above
        // expression:
        //
        // $version_id = $major_version * 10000 + $minor_version * 100 + $release_version;
        //
        // Now with PHP_VERSION_ID we can check for features this PHP version
        // may have, this doesn't require to use version_compare() everytime
        // you check if the current PHP version may not support a feature.
        //
        // For example, we may here define the PHP_VERSION_* constants thats
        // not available in versions prior to 5.2.7

        if (PHP_VERSION_ID < 50207) {
            define('PHP_MAJOR_VERSION',   $version[0]);
            define('PHP_MINOR_VERSION',   $version[1]);
            define('PHP_RELEASE_VERSION', $version[2]);

            // and so on, ...
        }
    }

    function phpVersionAtLeast($strVersion = '0.0.0')
    {
        $version = explode('.', $strVersion);

        $questionVer = $version[0] * 10000 + $version[1] * 100 + $version[2];

        if(PHP_VERSION_ID >= $questionVer)
            return true;
        else
            return false;

    }

    function openSession()
    {
        if(phpVersionAtLeast('5.4.0'))
        {
            if(session_status()==PHP_SESSION_NONE)
                session_start();
        }
        else // under 5.4.0
        {
            if(session_id() == '')
                session_start();
        }
    }
if (session_id() === "") { session_start(); }

希望能有所帮助!

PHP 5.4引入了session_status(),它比依赖session_id()更可靠。

考虑下面的代码片段:

session_id('test');
var_export(session_id() != ''); // true, but session is still not started!
var_export(session_status() == PHP_SESSION_ACTIVE); // false

因此,要检查会话是否已启动,PHP 5.4中推荐的方法是:

session_status() == PHP_SESSION_ACTIVE