Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stop PHP from sending data to client while still running PHP code in server?

Tags:

http

php

This question came up to me when I encountered a bug that caused my PHP program to loop infinitely. Here is an example situation:

Suppose I have a PHP webpage that receives picture uploads (the page perhaps is a response page for an image upload form). In the server, the script should store the image in a temporary file. The script should then output a confirmation message to the client then stop sending data so that the client would not wait. The script should then continue executing, processing the image (like resizing it) before ending.

I think this "technique" could be useful such that the client will not wait during time-consuming processes, therefore preventing time-outs.

Also, could this be solved using HTTP methods?

like image 747
Mark Garcia Avatar asked Aug 24 '12 06:08

Mark Garcia


2 Answers

Yes.

This can easily be done without any asynchronous processing if you correctly utilize HTTP headers.

Under normal conditions PHP will stop processing as soon as the client on the other end closes the connection. If you want to continue processing after this event, you need to do one thing: tell PHP to ignore user aborts. How?

ignore_user_abort()

This will allow your script to keep running even after the client gets the heck out of dodge. But we're also faced with the problem of how to tell the client that the request they made is finished so that it will close the connection. Normally, PHP transparently handles sending these headers for us if we don't specify them. Here, though, we need to do it explicitly or the client won't know when we want them to stop reading the response.

To do this, we have to send the appropriate HTTP headers to tell the client when to close:

Connection: close
Content-Length: 42

This combination of headers tells the client that once it reads 42 bytes of entity body response that the message is finished and that they should close the connection. There are a couple of consequences to this method:

  1. You have to generate your response BEFORE sending any output because you have to determine its content length size in bytes so you can send the correct header.
  2. You have to actually send these headers BEFORE you echo any output.

So your script might look something like this:

<?php

ignore_user_abort();

// do work to determine the response you want to send ($responseBody)
$contentLength = strlen($responseBody);

header('Connection: close');
header("Content-Length: $contentLength");
flush();

echo $responseBody;

// --- client will now disconnect and you can continue processing here ---

The big "Gotchya" with this method is that when you're running PHP in a web SAPI you can easily run up against the max time limit directive if you do time-consuming processing after the end user client closes the connection. If this is a problem, you may need to consider an asynchronous processing option using cron because there is no time limit when PHP runs in a CLI environment. Alternatively, you could just up the time limit of your scripts in the web environment using set_time_limitdocs.

It's worth mentioning that if you do something like this, you may also want to add a check to connection_aborted()docs while generating your response body so that you can avoid the additional processing if the user aborts before completing the transfer.

like image 80
rdlowrey Avatar answered Sep 25 '22 17:09

rdlowrey


I have facing the same problem when i upload image on twitter & facebook from iphone through web service of php.

If the processing time of image upload is not much then you can check the comment of @Musa this may help you but if it takes too much time to process then try this steps.

 1. Image store in folder
 2. Fetch image from folder using cron 
 3. Cron run for every 2 min in backend

these will decrease your processing time.

Hope this help you.

like image 41
Tony Stark Avatar answered Sep 23 '22 17:09

Tony Stark