Answer the question
In order to leave comments, you need to log in
PHP. Executing a script in the background or similar
Good day. The task is to generate and send a response to the browser, closing the connection with it, and then continue the script execution. It seems that the following code should work:
ob_end_clean();
header("Connection: close\r\n");
header("Content-Encoding: none\r\n");
ignore_user_abort( true ); // optional
ob_start();
echo ('Text user will see');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
ob_end_clean();
//do processing here
sleep(10);
echo('Text user will never see');
Answer the question
In order to leave comments, you need to log in
And in what place do you have "the connection with the browser is broken" here?
Write to the socket while running another script, and you'll be happy.
To be honest, I have already forgotten PHP a little, but if my memory fails me when the connection is broken, Apache\another web server will kill the PHP script stream. At least with the default settings. I'm hinting that if you find a way to kill the connection, are you sure that Apache won't kill your script?
If you need to do asynchronous processing, insert an invisible img at the end of the page, and src is a link to the script. The page will be loaded, the user is happy and the processing will go in the background. But there is no guarantee that the user will not close the browser until your processing is over (the picture will not “load”). You can show him the progress bar with Ajax. (One script, to which the link is in the picture, saws something in the background and writes progress to the database, the other gives it to the user via Ajax). In general, all these server processing in PHP is a bad thing. It is best to create a table in which you write everything that needs to be done asynchronously, and let the same php run every 10 seconds via cron on the server (at least through wget of a special URL) and do everything asynchronously. The last solution is the most stable. And eliminates the need to keep the browser open.
www.php.net/manual/en/features.connection-handling.php
The comments say to disconnect the browser and then continue processing:
header("Content-Length: 0");
header("Connection: close");
flush();
// browser should disconnect at this point
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question