Can a PHP script make the browser think that the HTTP request is complete?

At first I configured my script to run even after the HTTP request was completed

ignore_user_abort(true); 

then rinse the text.

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

Now, how can I trick the browser into thinking that the HTTP request has completed? so I can continue to do my own work without a browser, showing "page loading".

  header(??) // something like this? 
+6
scripting php webserver
source share
5 answers

Here's how to do it. You tell the browser to read the first N characters of the output and then close the connection while your script continues to run until it is executed.

 <?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'); ?> 
+10
source

Headers will not work (they are headers, so they come first)

I do not know how to close the http connection without ending the script, although I assume there is some obscure way to do this.

Telling us what you want to do after the request is completed, we can give the best deals.

But actually, I would think of one of the following:

1) Run a simple script command line (using exec ()) that looks like this:

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

Then remove this from your http-bound script, like:

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

Or:

2) Write another program (perhaps a constantly running daemon or just some kind of script that happens so often), and find out how your request code can pass instructions to it. You may have a database table in which the queue runs, or try to make it work with some kind of flat file. You can also use your web-based script to invoke some command line command, due to which your external script request should do some work.

At the end of the day, you do not want your script to continue executing after an HTTP request. Assuming you are using mod_php, this means that you will bind the apache process until the script completes.

+4
source

Perhaps this specific comment on the php.net man page will help: http://www.php.net/manual/en/features.connection-handling.php#71172

+4
source
Theoretically, if HTTP 1.1 keep-alive is enabled and the client receives the number of characters that it expects from the server, it should consider it as the end of the response and go ahead and display the page (while keeping the connection still open.) Try sending these headers (if you you cannot enable them in another way):
  Connection: keep-alive
 Content-Length: n

Where n is the number of characters sent to the response body (output buffering may help you count this.) Sorry, I don’t have time to check it myself. I just throw a suggestion if it works.

+2
source

The best way to achieve this is to use output buffering. PHP sends headers when it is good and ready, but if you complete the output to the browser using ob_ *, you can control the headers at every step.

You can hold the displayed page in a buffer, if you want, and send headers until the sun appears in China. This practice is that you can see many opening <?php tags, but there are currently no closing tags. It prevents the script from sending headers prematurely, as some of them may include.

+2
source

All Articles