发送标头后,如何通过SID更新数据会话?

时间:2018-12-27 00:51:30

标签: php session

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

<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 向我的域提出请求-我认为这不是一个好主意。

PS:英语不是我的母语

1 个答案:

答案 0 :(得分:0)

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