Continue PHP Execution After Sending Http Response

How to continue PHP script after sending HTTP response

In JS:

$(document).ajaxSuccess(function ( event, xhr, settings) {
if(xhr.status === 202) {
//console.log("Your file is in process");
//console.log(xhr.responseJSON);
//Activate interval to check if file is ready
}
});
//Make petition
$.post("petitionsHandler.php", {"texto":"Hello world, greeting from México!"}, function (resp){
//Handle responses
}, "json").fail(function () {
//Handle errors
});

In PHP

<?php
$text = $_POST["text"];
//If you are using sessions don't forget to close the session file
//Or such will be blocked until long script finishes
session_write_close();
header("HTTP/1.1 202 Solicitud recibida"); #This is the code I wanted to send
header("Content-Type: application/json"); #Depends the kind of data you're sending to the client
// Buffer all upcoming output...
ob_start();

// Send your response.
echo '{"success":true, "message":"Your request has been received."}';
// Get the size of the output.
$size = ob_get_length();

// Disable compression (in case content length is compressed).
//header("Content-Encoding: none"); #I didn't need this but check your situation

// Set the content length of the response.
header("Content-Length: {$size}");

// Close the connection.
header("Connection: close");

// Flush all output.
ob_end_flush();
ob_flush();
flush();
//All outputs have now been sent to the client
//You can continue executing your long tasks

include_once('createExcel.php');

createExcel.php

<?php
sleep(10); #You can use to checkthat the code works
//The above code will respond the client immediately and after ten seconds the Excel file will be created
require "PHPSpreadSheet/vendor/autoload.php";
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', $text);
$writer = new Xlsx($spreadsheet);
$writer->save('MyFile.xlsx');

Of course you have to make validations, so you don't have lots of running process in background but I hope the general idea has been shown in this example.

This is not exactly the code I use, but this is all it takes to continue executing code after responding the client.

In my own opinion, I thinks this is better than using exec() or similar functions which invoke the command terminal as that could be a potential vulnerability , so you don't have to change permissions or anything.

Note: If you're using sessions, Please remember to use session_write_close() because on heavy tasks will block the file until such task is finished.

I hope it helps :)

My answer was based on this blog: https://qastack.mx/programming/15273570/continue-processing-php-after-sending-http-response

Continue PHP execution after sending HTTP response

Have the script that handles the initial request create an entry in a processing queue, and then immediately return. Then, create a separate process (via cron maybe) that regularly runs whatever jobs are pending in the queue.

PHP with AJAX - Continue Processing After Sending Response to the User

It looks like (as @ADyson said in the comment) flushing output doesn't seem work with AJAX. So, the only solution to the problem is to use Cron Job. To do this, I used PHP Cron Scheduler. But crontab can be used directly to add a Cron Job to the list.

I added the post processes to another PHP file with suitable changes so that it can do the same tasks from the script and added the file to the Scheduler.

This solution may seem a real pain, but there is no solution to this problem other than using Cron Job.

Continue processing after closing connection

I finally found a solution (thanks to Google, I just had to keep trying different combinations of search terms). Thanks to the comment from arr1 on this page (it's about two thirds of the way down the page).

<?php
ob_end_clean();
header("Connection: close");
ignore_user_abort(true);
ob_start();
echo 'Text the user will see';
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush(); // All output buffers must be flushed here
flush(); // Force output to client
// Do processing here
sleep(30);
echo('Text user will never see');

I have yet to actually test this but, in short, you send two headers: one that tells the browser exactly how much data to expect then one to tell the browser to close the connection (which it will only do after receiving the expected amount of content). I haven't tested this yet.

PHP - Send response and continue execution until next response

Only by adding a refresh meta tag in the html (http://webmaster.iu.edu/tools-and-guides/maintenance/redirect-meta-refresh.phtml) or by using javascript.

Edit: To redirect with javascript see How to redirect to another webpage in JavaScript/jQuery?



Related Topics



Leave a reply



Submit