PHP session_write_close()导致空响应

时间:2010-03-29 00:20:15

标签: php session

在脚本末尾的关闭函数中使用session_write_close()时 - PHP就会死掉。没有记录错误,响应头(firebug)或数据(甚至是空格!)返回。我在启用STRICT和PHP 5.2.1时有完整的PHP错误报告。

我的猜测是,因为在关闭后调用了session_write_close() - 遇到一些致命的错误,它会在PHP有机会发送输出或记录任何内容之前崩溃。

这只发生在我首先登出的退出页面上:

...
    //If there is no session to delete (not started)
    if ( ! session_id())
    {
        return;
    }

    // Get the session name
    $name = session_name();

    // Delete the session cookie (if exists)
    if ( ! empty($_COOKIE[$name]))
    {
        //Get the current cookie config
        $params = session_get_cookie_params();

        // Delete the cookie from globals
        unset($_COOKIE[$name], $_SESSION);

        //Delete the cookie on the user_agent
        setcookie($name, '', time()-43200, $params['path'], $params['domain'], $params['secure']);
    }

    // Destroy the session
    session_destroy();
...

然后2)做更多的事情3)发出重定向和4)最后,在整个页面完成后,我放置了register_shutdown_function();之前的运行并调用session_write_close()将会话保存到数据库。结束。

由于此空白响应仅在注销时发生,我猜测我没有正确地重新启动会话,导致session_write_close()在脚本结束时死亡。

1 个答案:

答案 0 :(得分:0)

怪异。问题似乎是我在删除cookie之前破坏了会话。

这有效:

    // Delete the session cookie (if exists)
    if ( ! empty($_COOKIE[$name]))
    {
        //Get the current cookie config
        $params = session_get_cookie_params();

        // Delete the cookie from globals
        unset($_COOKIE[$name], $_SESSION);

        //Delete the cookie on the user_agent
        setcookie($name, '', time()-43200, $params['path'], $params['domain'], $params['secure']);
    }

    // Destroy the session -----------------------------------------
    session_destroy();

虽然这会杀死页面:

    // Destroy the session -----------------------------------------
    session_destroy();

    // Delete the session cookie (if exists)
    if ( ! empty($_COOKIE[$name]))
    {
        //Get the current cookie config
        $params = session_get_cookie_params();

        // Delete the cookie from globals
        unset($_COOKIE[$name], $_SESSION);

        //Delete the cookie on the user_agent
        setcookie($name, '', time()-43200, $params['path'], $params['domain'], $params['secure']);
    }

有谁知道为什么?