Here are few example of how to using it.
The first is basic example.
<?php
$file = __DIR__ . '/text.txt';
if (is_file($file) && is_writable($file)) {
@unlink($file);
echo '<small style="color: #ccc;">' . $file . ' was deleted.</small><br>' . PHP_EOL;
}
echo '<p>Calling to <code>fastcgi_finish_request()</code>.</p>' . PHP_EOL;
echo '<p>If success, the file ' . $file . ' will be created.</p>' . PHP_EOL;
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
} else {
echo '<p style="color: red;">This server does not support <code>fastcgi_finish_request()</code> function.</p>' . PHP_EOL;
echo 'Exit now.<br>' . PHP_EOL;
exit();
}
echo 'This line will be not echo out.<br>' . PHP_EOL;
file_put_contents($file, date('Y-m-d H:i:s') . PHP_EOL, FILE_APPEND);
?>
The file text.txt will be create if successfully.
==========================
The second is about execution timeout.
<?php
set_time_limit(5);
$file = __DIR__ . '/text.txt';
if (is_file($file) && is_writable($file)) {
@unlink($file);
echo '<small style="color: #ccc;">' . $file . ' was deleted.</small><br>' . PHP_EOL;
}
echo '<p>Testing timeout and <code>fastcgi_finish_request()</code> function.</p>' . PHP_EOL;
echo '<p>Set timeout to ' . ini_get('max_execution_time') . ' seconds.</p>' . PHP_EOL;
echo '<p>Calling to <code>fastcgi_finish_request()</code>.</p>' . PHP_EOL;
echo '<p>If success, the file ' . $file . ' will be created but error will be shown in the log.</p>' . PHP_EOL;
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
} else {
echo '<p style="color: red;">This server does not support <code>fastcgi_finish_request()</code> function.</p>' . PHP_EOL;
echo 'Exit now.<br>' . PHP_EOL;
exit();
}
$i = 1;
while(true){
if ($i <= 10) {
file_put_contents($file, date('Y-m-d H:i:s') . PHP_EOL, FILE_APPEND);
$i++;
}
}
?>
I found that the code will be working as long as it is not reach timeout setting in php.ini or set_time_limit() function.