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

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

问题描述

我怎样重新打开没有得到头已经发出警告,在PHP会话?

设置所有会话瓦尔我喜欢设置完成后,我关闭与 session_write_close会议()。我这样做,因为只要会话是打开的,有可能是来自同一客户只有一个活动的连接。但我喜欢有多个并行的。

但是,如果我想在以后设置另一个会话变量,我需要重新打开的在session_start()一次。这工作,但我已经送code到它打印客户端的头已经发出-warnings。为什么再次尝试设置Cookie?该Cookie已设置。我唯一​​需要的是获得再次写入服务器上的会话文件的访问。

好吧,我可以燮preSS他们。但有重开对话的一种方式,已关闭与session_write_close没有饼干头送重? Cookie的头已经通过第一在session_start正确发送()。所以,第二个只需要还给我访问写入存储在Web服务器上的会话文件。

 < PHP
在session_start();
//设置所有会话瓦尔我喜欢设置
session_write_close(); //&下; - //为了允许由相同的用户并行请求,而这个脚本仍在运行// code,需要一些时间来执行
//它还打印输出,所以这点后,没有更多的饼干头@session_start(); //< - 工作,但我喜欢用它,而不燮pressing警告
$ _SESSION ['关键'] ='新的价值我想将';
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(); //第二次在session_start

这将prevent从PHP调用 php_session_send_cookie()第二次。结果
看到它的工作

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

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

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.

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.

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

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

Though restructuring the scripts still seems to be the better option...

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

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