在 PHP 中重新打开会话 [英] Reopening a session in PHP

查看:23
本文介绍了在 PHP 中重新打开会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 PHP 中重新打开会话而不收到标题已发送警告?

How do I reopen a session in PHP without getting header already sent warnings?

设置好我喜欢设置的所有会话变量后,我用 session_write_close().我这样做是因为只要会话处于打开状态,可能只有一个来自同一客户端的活动连接.但我喜欢有多个并行的.

After setting all the session vars I like to set, I close the session with session_write_close(). I do this, because as long as the session is open, there may be only one active connection from the same client. But I like to have multiple parallel ones.

但是,如果我想在以后设置另一个会话变量,我需要使用 session_start() 再次.这有效,但由于我已经将代码发送到客户端,它会打印已发送标头"-警告.为什么它试图再次设置 cookie?cookie 已经设置.我唯一需要的是获得再次在服务器上写入会话文件的权限.

However if I like to set another session variable at a later point, I need to reopen the session with session_start() once again. This works, but as I already send code to the client it prints "headers already sent"-warnings. Why is it trying to set the cookie again? The cookie is already set. Only thing I need is to gain access to writing the session files on the server again.

好吧,我可以压制它们.但是有没有办法重新打开会话,该会话已被 session_write_close 关闭而不重新发送 Cookie 标头?第一个 session_start() 已经正确发送了 Cookie 头.所以第二个只需要让我重新获得写入存储在网络服务器上的会话文件的权限.

Well, I can suppress them. But is there a way of reopening a session, that has been closed with session_write_close without re-sending the Cookie-header? The Cookie-header is already sent correctly by the first session_start(). So the second one just needs to give me back access to writing to the session files stored on the web server.

<?php
session_start();
// setting all the session vars I like to set
session_write_close(); // <-- // To allow parallel requests by the same user, while this script is still running

// Code that takes some time to execute
// It also prints output, so no more cookie headers after this point

@session_start(); // <-- works, but I like to use it without suppressing warnings
$_SESSION['key'] = 'new value I like to store';
session_write_close();
?>

推荐答案

session_start();
...
session_write_close();
...

ini_set('session.use_only_cookies', false);
ini_set('session.use_cookies', false);
ini_set('session.use_trans_sid', false);
ini_set('session.cache_limiter', null);
session_start(); // second session_start

这将阻止 php 再次调用 php_session_send_cookie().
看看效果.

This will prevent php from calling php_session_send_cookie() a second time.
See it working.

虽然重组脚本似乎仍然是更好的选择...

对于 PHP 7.2+,您基本上需要重新实现会话 cookie 以避免错误和警告:

For PHP 7.2+, you will basically need to re-implement session cookies to avoid errors and warnings:

ini_set('session.use_only_cookies', false);
ini_set('session.use_cookies', false);
ini_set('session.use_trans_sid', false);
ini_set('session.cache_limiter', null);

if(array_key_exists('PHPSESSID', $_COOKIE))
    session_id($_COOKIE['PHPSESSID']);
else {
    session_start();
    setcookie('PHPSESSID', session_id());
    session_write_close();
}

session_start();
...
session_write_close();
...

session_start(); // second session_start

这篇关于在 PHP 中重新打开会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆