S
S
Stepan Zubashev2012-01-28 20:06:00
PHP
Stepan Zubashev, 2012-01-28 20:06:00

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');

It works locally, but on the server (not virtual hosting, it is possible to edit configs) - no. The problem is the following - the connection with the browser is not broken. Those. in the above example, the user will still have to wait 10 seconds, but he will not see 'Text user will never see'. What's the catch? What parameters in php.ini should be changed?

PS local - Ubuntu 11.04, PHP 5.3.5-1ubuntu7.3; online - Debian, PHP 5.2.6-1+lenny9

Answer the question

In order to leave comments, you need to log in

7 answer(s)
D
DevMan, 2012-01-28
@faiwer

php.net/manual/en/function.register-shutdown-function.php

W
werdender, 2012-01-28
@werdender

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.

F
fozzy, 2012-01-28
@fozzy

ua2.php.net/manual/en/install.fpm.php
fastcgi_finish_request()

A
Anatoly, 2012-01-28
@taliban

why don't you try forms instead?

D
Dmitry Guketlev, 2012-01-28
@Yavanosta

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.

L
lashtal, 2012-01-29
@lashtal

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

Z
Zyava, 2012-01-29
@Zyava

And that doesn't work either?
set_time_limit(0);
ignore_user_abort(true);
header("Connection: close");
ob_flush();
flush();
// do processing here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question