I need to run post connection close processing (sending emails, refreshing caches etc) which take a long time. So in order to do this I have an action helper which will eventually down the line will check if anything needs to be done and process it.
Here's a simplified version of the output bit:
$this->getFrontController()->returnResponse(false);
$response = $this->getResponse();
$body = $response->getBody();
$response->setHeader('Connection', 'close');
ob_start();
echo $body;
$size = ob_get_length();
$response->setHeader('Content-length', $size);
ob_end_flush();
flush();
$this->run();
(Note, when live I intend on using fastcgi_finish_request but this also needs to work locally. $this->run() runs the post processing functions, no output here).
I keep getting the ol' hated Headers Sent error with this, but the exception returns no file or line number where it happened (I am guessing this is because of the nature of action helpers in Zend).
Some digging has got the error down to the setHeader() call. But I thought with returnResponse(false) they wouldn't auto send?
Is my logic to output this correct?
Edit:
Another issue is that getReponse doesn't return anything because the layout render hasn't been called, but calling Zend_Layout::getMvcInstance()->render(); gives me the layout with no content rendered to $layout->content ?
I guess ob_end_flush() sends $body, etc. but $this->run() tries to send the headers afterwards. Try to use something like $content = ob_get_contents(); and ob_end_clean() to save the buffer content to a variable and to clear the buffer without sending. Then, you can echo $content after sending your headers.
Related
I'm working on speeding up the response time of some php code that generates html. One of the issues with the code, is that when determines a piece of information does not need to be displayed, it makes a sql call to delete the item from the database. This isn't visible to the user, and the won't be visible to the server until the next time the page is loaded, so that sql query does not need to be run as soon as the system knows that it should be run.
What I would like to do is return the response to the user, with the generated html, and then make the sql queries. I was trying this flush and ob_flush, but the page response is still not loaded until I make a call to die.
Is there anyway in PHP to run code after a call to die() so that the user gets their data and then I can run my database clean up code and the client is no long waiting on me to close the connection?
You can register shutdown functions using register_shutdown_function:
register_shutdown_function(function () {
// cleanup stuff
});
Or in older versions of PHP:
function myFunc() {
// cleanup stuff
}
register_shutdown_function("myFunc");
Thanks to #robbrit and #Luis Siquot. I was looking at register_shutdown_function and because of Luis' comment, I was reading the comments on that page, and I came across "When using php-fpm, fastcgi_finish_request() should be used instead of register_shutdown_function() and exit()"
which lead me to fastcgi_finish_request which says:
"This function flushes all response data to the client and finishes the request. This allows for time consuming tasks to be performed without leaving the connection to the client open."
So it looks like fastcgi_finish_request() is what I'm looking for, not register_shutdown_function()
Edit: It seems that fastcgi_finish_request() requires another library, so instead use:
ob_end_clean();
header("Connection: close");
ignore_user_abort(true); // just to be safe
ob_start();
echo "The client will see this!";
$size = ob_get_length();
header("Content-Length: $size");
//Both of these flush methods must be called, otherwise strange things happen.
ob_end_flush();
flush();
echo "The client will never see this";
headers_sent will tell me when output was sent to the browser. However, it can be that no body was sent, e.g. 302 redirect.
How do I tell in register_shutdown_function context what content has been sent to the browser, or at least what was Content-Length.
The content lenght header will no being set by apache if it is a PHP script. This is because the webserver can't know about this as the content is dynamically created. However the headers have to sent before the body. So the only way to get the length of the content is to generate it, obtain it's length, send the header and then send the content.
In PHP you can use ob_* set of functions (output buffering) to achieve this. Like this:
ob_start();
echo 'hello world'; // all your application's code comes here
register_shutdown_function(function() {
header('Content-Length: ' . ob_get_length());
ob_end_flush();
});
Warning This will be unreliable if you are using gzip encoded transfer. There has been posted a workaround on the PHP web site,
Also you might need to know that output buffers can be nested in PHP. If there is another ob_start() call in my example above than you'll end up seeing nothing in the browser because just the inner buffer gets flushed (into the outer one)
The following example takes care on this. In order to ease the process it just overwrites the header multiple times, which shouldn't be a performance issue as header() is basically a simple string operation. PHP sends the headers only before some output or at the end of the script.
Here comes the code which has been tested gzip safe and works reliable with nested unclosed buffers:
ob_start('ob_gzhandler');
echo 'hello world';
// another ob_start call. The programmer missed to close it.
ob_start();
register_shutdown_function(function() {
// loop through buffers and flush them. After the operation the
// Content-Lenght header will contain the total of them all
while(ob_get_level()) {
header('Content-Length: ' . ob_get_length(), TRUE);
ob_end_flush();
}
});
I am running a script every night and the output of the script will only be send to a mail address. But the problem is that I need to receive a copy of the output in my own mailbox. I registered an shutdown handler in the script and I tried to send a mail with functions like ob_get_contents which actually shows data. But only the last thing I printed to the terminal.
cronMail('Cron', ob_get_contents());
The function called is just a simple function which adds the default receiver and sender and call the PHP Mail function.
The output in the mail is:
array()
While the terminal has te following output:
Starting cron...
Exiting...
array()
Can anyone tell me how to receive the whole output? I started the output buffer by using the ob_start method. And after each line I make sure there is an ob_flush method called so the output will also be send to the browser if the script is called directly.
ob_flush stands in your way, see the linked description on the manual page, it is pretty clear about that: It flushes the buffer so outputs it.
You do not want that. Remove the calls to it and you should be fine.
ob_start();
... your script without "ob_flush()" ...
$buffer = ob_get_clean(); # finally get the output buffer as string
echo $buffer; # pass output along for cron
cronMail('Cron', $buffer); # send your mail
This variant ensures that you get your own email but also the output is passed along to cron. This can be useful if you do some error reporting in the cronMail function, so that at least there is some way to further debug that.
Another alternative is to register an output handling function that stores the output on the go. But that is less trivial so I keep it out.
I have a ob_start() and a corresponding ob_flush(). I would like to flush a portion of data and continue executing the rest. Using ob_flush() didn't help. Also if possible rest needs to happen without showing loading in browser.
EDIT:
I don't want to use ajax
I have done this in the past and this is how I solved it:
ob_start();
/*
* Generate your output here
*/
// Ignore connection-closing by the client/user
ignore_user_abort(true);
// Set your timelimit to a length long enough for your script to run,
// but not so long it will bog down your server in case multiple versions run
// or this script get's in an endless loop.
if (
!ini_get('safe_mode')
&& strpos(ini_get('disable_functions'), 'set_time_limit') === FALSE
){
set_time_limit(60);
}
// Get your output and send it to the client
$content = ob_get_contents(); // Get the content of the output buffer
ob_end_clean(); // Close current output buffer
$len = strlen($content); // Get the length
header('Connection: close'); // Tell the client to close connection
header("Content-Length: $len"); // Close connection after $len characters
echo $content; // Output content
flush(); // Force php-output-cache to flush to browser.
// See caveats below.
// Optional: kill all other output buffering
while (ob_get_level() > 0) {
ob_end_clean();
}
As I said in a couple of comments before, you should watch out for gzipping your content, since that will alter the length of your content, but not change the header about it. It also can buffer your output, so it won't get send to the client instantly.
You could try letting apache know to not gzip your content by using apache_setenv('no-gzip', '1');. But this will not work if you use rewrite-rules to go to your page, since then it will also modify those environment variables. At least, it did so for me.
See more caveats about flushing your content to the user in the manual.
ob_flush writes the buffer. In other words, ob_flush tells PHP to give Apache (or nginx/lighttpd/whatever) the output and then for PHP to forget about it. Once Apache has the output, it does whatever it wants with it. (In other words, after ob_flush it's out of your control whether or not it gets immediately written to the browser).
So, short answer: There's no guaranteed way to do that.
Just a guess, you're likely looking for AJAX. Whenever people are trying to manipulate when page content loads as you're doing, AJAX is almost always the correct path.
If you want to continue a task in the background, you can use ignore_user_abort, as detailed here, however, that is often not the optimal approach. You essentially lose control over that thread, and in my opinion, a web server thread is not where heavy processing belongs.
I would try to extract it out of the web facing stuff. This could mean a cron entry or just spawning a background process from inside of PHP (a process that though started from inside of script execution will not die with the script, and the script will not wait for it to finish before dying).
If you do go that route, it will mean that you can even make some kind of status system if necessary. Then you could monitor the execution and give the user periodic updates on the progress. (Technically you could make a status system with a ignore_user_abort-ed script too, but it doesn't seem as clean to me.)
this is my function
function bg_process($fn, $arr) {
$call = function($fn, $arr){
header('Connection: close');
header('Content-length: '.ob_get_length());
ob_flush();
flush();
call_user_func_array($fn, $arr);
};
register_shutdown_function($call, $fn, $arr);
}
wrap the function to be executed in the end, after php close the connection. and of course the browser will stop buffering.
function test() {
while (true) {
echo 'this text will never seen by user';
}
}
this is how to call the function
bg_process('test');
first argument is callable,
second argument is an array to be passed to 'test' function with an indexed array
Note : I don't use ob_start() at the beginning of the script.
I have an article explaining how this can be achieved using apache/mod_php on my blog here: http://codehackit.blogspot.com/2011/07/how-to-kill-http-connection-and.html Hope this helps, cheers
If you are using PHP-FPM:
ignore_user_abort(true);
fastcgi_finish_request();
Above two functions are the key factors which ignore_user_abort prevents error and fastcgi_finish_request closes client connection.
fastcgi_finish_request
This function flushes all response data to the client and finishes the request. This allows for time consuming tasks to be performed without leaving the connection to the client open.
not working on Apache.(PHP 5 >= 5.3.3, PHP 7)
Use:
header("Content-Length: $len");
..where $len is the length of the data to be flushed to the client.
I don't have the background to know when and where this is going to work, but I tried on a few browsers, and all returned instantly with:
<?PHP
header("Content-length:5");
echo "this is more than 5";
sleep(5);
?>
edit: Chrome, IE, and Opera showed this, while FireFox showed this is more than 5. All of them closed the request after that though.
Final update
Seems like I did make a very simple error. Since I already have a stream implementation I can just not start reading from the stream :D
I'm trying to achieve fire-and-forget like functionality in PHP.
From php.net
<?php
ignore_user_abort(true);
header("Content-Length: 4");
header("Connection: Close");
echo "abcd";
flush();
sleep(5);
echo "Text user should not see"; // because it should have terminated
?>
This works if I open the script with a browser. (shows "abcd").
But if I open it with file_get_contents or some stream library it will wait for ~5 seconds and show the second text as well.
I'm using PHP 5.2.11 / Apache 2.0
Update
I seems there is some confusion about what I'm trying to accomplish.
I don't want to hide output using output buffers (that's stupid). I want to have the client terminate before the server starts a possibly lengthy process (sleep(5)) and I don't want the client to wait for it (this is what fire-and-forget means, sort off).
The use of output buffers is merely a side effect. I've amended the sample code without the use of output buffers.
What I don't understand is: why does this script behave differently when accessing it from the browser vs. fetching it in PHP with file_get_contents("http://dev/test.php") or some stream library? What I've seen in testing is that for instance stream_get_contents will actually block for 5 seconds before it returns any output at all, the is quite the opposite of what I want.
Update2
Some more results:
The browser somehow responds to the flush(). I can't figure out how to replicate this behavior with streams in PHP, my streams keep blocking.
I've tried fread and found that it behaves similar to stream_get_contents.
Specifying a maxlength has no effect, it will still block for ~5 seconds.
Changing the blocking mode has no effect (other than generating a bunch more calls to stream_get_contents()). It will wait ~5 seconds before returning anything.
stream_set_read_buffer has no effect (tested on a PHP 5.3.5 sever)
The second portion of text is showing up because you're stopping output buffering with ob_end_flush() and ob_end_clean(). When that happens PHP outputs content as normal. Try something like the following:
<?php
ob_start(); // turn on output buffering
print "Text the user will see.";
ob_flush(); // send above output to the user and keep output buffering on
print "Text the user will never see";
ob_end_clean(); // empty the buffer and turn off output buffering. your script should end here.
?>
It's important for ob_end_clean() to appear at the end of the script. It empties the buffer and does not send its contents to the user, thus keeping everything after ob_flush() hidden.
How do you access the script using file_get_contents? How do you access it with your browser? If you access the script without "http://", of course it will never get executed. Use the same URL as in the browser.
Edit:
The browser will render the page even before the connection is closed. Even if you flush, I don't think the connection is closed. You can fire up Wireshark and check. stream_get_contents and file_get_contents will block until they have all the output. Even if you flushed, they can't be sure that there isn't more content. Since the content-length header didn't seem to make {file,stream}_get_contents return earlier, you probably need to implement your own buffering, ala. fopen, read, fclose.
Seems like I did make a very simple error. Since I already have a stream implementation I can just not start reading from the stream :D