如何在发送标头后按 SID 更新数据会话



有时,在用户请求后,需要在后台保持运行当前脚本以处理有关更新的一些事件。特定事件必须更新用户会话上的某些密钥。我的代码:

<php 
ini_set('session.save_path',__DIR__ . '/../!PHPSESSID');
session_start();

// ...... page content - db queries and other stuff. here can be generated some events
//close user connection and keep running script in background
ignore_user_abort(true);
//fastcgi_finish_request(); -- no need in my case
header('Connection: close');
header('Content-Length: '.ob_get_length());
session_write_close();
ob_end_flush();
flush();
//OK, user connection are closed. This script is running now in bacground
set_time_limit(120); // for me is more than enough - OR set 0
//... my database store all sids by each user id for each connections (e.g. same user, by id 123, have connections: 2 from pc(difrent browser), 1 from mobile .... ). I always know how ~ many connections user have and how to find him to send some events
if($haveSomeSpecificEvents){
    foreach($specificEvents as $item){
        if(!file_exists(session_save_path().'/sess_'.$item['sid'])){
            continue; //skip
        }
        session_id($item['sid']); // generate warning: headers already sent
        session_start(); // generate warning: headers already sent
        //check if session is what i am looking for like $_SESSION['id'] == $item['user_id'], if not - just skip this
        //place some updates (flag) in $_SESSION, but is always NULL.
        session_write_close(); // save changes
    }
 }

当然,我可以使用 CURL 向我的域请求 - 我认为这不是一个好主意。

附言:英语不是我的第一语言

好吧,我等着有人回答我的问题,但我必须自己回答。也许这会帮助某人。我的解决方案:打开输出缓冲。只需在脚本顶部添加ob_start();即可。

最新更新