Maintaining PHP Execution After HTTP Response
Maintaining PHP execution beyond the HTTP response requires special considerations, particularly in environments like mod_php. To address this challenge, the following solution is presented:
To send an HTTP response while continuing PHP execution, you can leverage the following code snippet:
<?php ob_end_clean(); header("Connection: close"); ignore_user_abort(); // Optional ob_start(); echo('Text the user will see'); $size = ob_get_length(); header("Content-Length: $size"); ob_end_flush(); // Enable strange behavior flush(); // Required for behavior to work session_write_close(); // Suggested enhancement to ensure session data is saved // Perform processing here sleep(30); echo('Text user will never see'); ?>
This code performs the following steps:
The above is the detailed content of How to Maintain PHP Execution After Sending an HTTP Response?. For more information, please follow other related articles on the PHP Chinese website!