1

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

2
  • Don't. This is a bad idea. The only thing that should ever touch session data is PHP, and anything outside of that is going to result in missing/corrupted session data. Whatever you're trying to update likely shouldn't live in the session data in the first place.
    – Sammitch
    Commented Dec 27, 2018 at 0:56
  • Thank you Sam, but i have no choise. As i know, only one connections can be handle same session - here can't be corrupded data. Thats why, i always call session_write_close as soon as possible when script may running longer (bkg part code).
    – Artur
    Commented Dec 27, 2018 at 1:13

1 Answer 1

0

Well, I waited for someone to answer my question, but I had to answer it myself. Maybe this will help someone. My solution: turn on output buffering. Just add ob_start(); on top the script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.