Question

I first configure my script to run even after the HTTP request is over

 ignore_user_abort(true);

then flush out some text.

 echo "Thats all folks!";
 flush();

Now how can I trick the browser into thinking the HTTP request is over? so I can continue doing my own work without the browser showing "page loading".

 header(??) // something like this?
Was it helpful?

Solution

Here's how to do it. You tell the browser to read in the first N characters of output and then close the connection, while your script keeps running until it's done.

<?php
ob_end_clean();
header("Connection: close");
ignore_user_abort(true); // optional
ob_start();
echo ('Text the user will see');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();     // Will not work
flush();            // Unless both are called !

// At this point, the browser has closed connection to the web server

// Do processing here
echo('Text user will never see');
?>

OTHER TIPS

Headers won't work (they're headers, so they come first)

I don't know of any way to close the http connection without terminating the script, though I suppose there's some obscure way of doing it.

Telling us what you want to do after the request is done would help us give better suggestions.

But generally, I'd be thinking about one of the following:

1) Execute some simple command-line script (using exec()) that looks like:

#!/bin/sh
php myscript.php <arg1> <arg2> .. <argN> &

Then kick that off from your http-bound script like:

<?PHP
exec('/path/to/my/script.sh');
?>

Or:

2) Write another program (possibly a continuously-running daemon, or just some script that is cronned ever so often), and figure out how your in-request code can pass it instructions. You could have a database table that queues work, or try to make it work with a flat file of some sort. You could also have your web-based script call some command-line command that causes your out-of-request script to queue some work.

At the end of the day, you don't want your script to keep executing after the http request. Assuming you're using mod_php, that means you'll be tying up an apache process until the script terminates.

Maybe this particular comment on php.net manual page will help: http://www.php.net/manual/en/features.connection-handling.php#71172

Theoretically, if HTTP 1.1 keep-alive is enabled and the client receives the amount of characters it expects from the server, it should treat it as the end of the response and go ahead and render the page (while keeping the connection still open.) Try sending these headers (if you can't enable them another way):

Connection: keep-alive
Content-Length: n

Where n is the amount of characters that you've sent in the response body (output buffering can help you count that.) I'm sorry that I don't have the time to test this out myself. I'm just throwing in the suggestion in case it works.

The best way to accomplish this is using output buffering. PHP sends the headers when it's good and ready, but if you wrap your output to the browser with ob_* you can control the headers every step of the way.

You can hold a rendered page in the buffer if you want and send headers till the sun comes up in china. This practice is why you may see a lot of opening <?php tags, but no closing tags nowadays. It keeps the script from sending any headers prematurely since there might some includes to consider.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top