Basel Khatib
Basel Khatib

Reputation: 53

How to forcing set header content length?

I have a big PHP file, It takes a long time to execute. so I want to set Content-Length = 26. for don't wait until the end. But the problem is The server always set 'Content-Length' as actually Length. How can I forcing Content-Length?


header('Connection: close');
header('Cache-control: no-store');
// set Content-Length for just 26
header('Content-Length: 26');
echo 'Received and Processing...';
flush();ob_flush();


//// some codes...
//// some codes...
//// some codes...

echo 'end!';

I try on Local server the output: Received and Processing...

but in my website server, the output is: Received and Processing...end!

how can I solve the problem.

Upvotes: 0

Views: 1408

Answers (2)

symcbean
symcbean

Reputation: 48387

If I have understood what you are saying in your question and subsequent comments, you want the server to acknowledge the action sooner and not wait for the processing to complete.

Tinkering with http headers is not the right way to do this. Assuming that you are not interested in any new information which might arise during processing, then there are various ways to achieve this, but you need to dissociate execution of the processing from the navigation thread.

If you trigger a second PHP process (either using the first PHP process to make an http request or from JavaScript at the browser) and set a timeout on that operation, and begin the second script with ignore_user_abort(true) then you will have achieved that. There are other solutions.

But if the delay is due to bandwidth limits you have no choice but to wait.

Upvotes: 1

Maximilian Haye
Maximilian Haye

Reputation: 106

The HTTP Content-Length header is not a timeout. It tells the receiver of an HTTP message how many bytes the body of the message contains so that the receiver knows when to stop reading more bytes from the connection. If the Content-Length header contains a wrong value, things will break. You should never have to set that header manually.

To limit execution time of a PHP script, the function set_time_limit and the global config variable max_execution_time seem to be your options.

Upvotes: 2

Related Questions