1

session_start()升级到 php7.2 后调用时出现新错误:

PHP 警告:session_start():在第 20 行的 Example.php 中已发送标头时无法启动会话

此警告阻止写入会话。这发生在我用于事件源(服务器发送事件)的长时间运行的 php 脚本中。为了响应某些事件,脚本应该更新会话值。此代码适用于 php7。

例子:

header("Content-Type: text/event-stream\n\n");
session_start();
session_write_close();

$i = 0;
while(true){

sleep(3);
// At some point new information is retrieved

$NewValue = $i++;

if ($NewValue % 2 == 0){
$SomethingInterestingHappened = true;
}else{
$SomethingInterestingHappened = false;
}

if($SomethingInterestingHappened){
session_start();  // <- this line throws error
$_SESSION['CurrentRecord'] = $NewValue;
session_write_close();
}

// Writes cool things to browser

}

有谁知道使用本机 php7.2 进行这项工作的方法?或对第三方解决方案的建议?

我发现了可能相关的错误报告。

更新:添加了工作代码示例,以防有人想要测试它。

4

0 回答 0