Sometimes, after user request, is necesary to keep run current script in background to process some events about updates. Specific events must update some keys on user session. My code:
<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
}
}
Of cource, i can do request using CURL to my domain - i think this is not a good ideea.
P.S: English is not my first language