If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Script execution in background

Started by maxi007, Mar 13, 2023, 12:07 AM

Previous topic - Next topic

maxi007Topic starter

Hello. The task is to form a response to the browser, close the connection, and continue executing the script. This can be achieved by using the provided code snippet.

ob_end_clean();
header("Connection: close\r\n");
header("Content-Encoding: none\r\n");
ignore_user_abort( true ); // optional
ob_start();
echo ('Text user will see');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
ob_end_clean();

//do processing here
sleep(10);
echo('Text user will never see');

This code works locally, but on the server (not shared hosting, where editing configurations is possible) it doesn't work as intended. The problem is that the connection with the browser is not being closed. In other words, in the given example, the user will still have to wait for 10 seconds, but they will not see the text "Text user will never see".

To solve this problem, you need to modify the php.ini file. Specifically, you should look for and change certain parameters.
  •  


dozerin

Where exactly do you see that "the connection with the browser is broken" in this context? If you write to the socket and execute another script, everything should work fine.

It's important to understand the different components involved in establishing and maintaining a connection between a browser and a server. By properly handling the socket connections and executing the necessary scripts, you can ensure a smooth and seamless experience for the user.
  •  

garrylord

To be honest, my knowledge of PCP is a bit rusty, but if I recall correctly, when the Apache connection is broken, another web server will terminate the thread running the PCP script. This is typically the case with default settings. I'm suggesting that if you manage to terminate the connection, are you certain that Apache won't terminate your script as well?

If your goal is to perform asynchronous processing, you can achieve it by inserting an invisible image at the end of the page and setting its source to a link pointing to the script. This way, the page will load, satisfying the user, while the processing happens in the background. However, there's no guarantee that the user won't close the browser before the processing completes (resulting in the image not loading).

One approach to address this issue is to show a progress bar using Ajax. One script, referenced by the image, performs background tasks and updates the progress in the database, while the other script retrieves the progress information for the Ajax user. Generally speaking, server-side processing in PHP can be challenging. It's often better to have a task queue where you can list all the asynchronous tasks and have a PHP script run at regular intervals (e.g., every 10 seconds) via cron or a special wget URL to process them asynchronously. This solution is more stable and eliminates the need to keep the browser open.

It's essential to consider the practical limitations and trade-offs when implementing server-side processing, as it can impact the overall user experience and system reliability.
  •  

superjohn

Doesn't this solution work either?

set_time_limit(0);

ignore_user_abort(true);

header("Connection: close");

ob_flush();

flush();

// do processing here

This code snippet aims to optimize the execution of a script by setting the time limit, ignoring user aborts, and flushing the output buffer. However, if this approach doesn't yield the desired results, there may be other factors at play.

It is important to consider the specific requirements of your situation and potentially explore alternative methods or configurations to ensure a smooth execution of your script.
  •  

prctshplc

To solve the problem of the connection not being closed in this scenario, you would need to modify certain parameters in the php.ini file. Here are the steps you should follow:

1. Locate and open the php.ini file on your server.
2. Look for the "max_execution_time" parameter and set it to a lower value, such as 5 seconds. This will ensure that the script execution is terminated after a specified amount of time.
3. Set the "output_buffering" parameter to "On" or increase its value. This will enable output buffering and allow the script to continue executing while still sending data to the browser.
4. Increase the value of the "memory_limit" parameter if needed. This will ensure that your script has enough memory to execute without any issues.
5. Save the changes to the php.ini file and restart your server for the modifications to take effect.

If modifying the php.ini file is not an option, there are alternative methods you can try to achieve the desired outcome of closing the connection with the browser and continuing script execution. Here are a few possible approaches:

1. Use JavaScript: You can use JavaScript to make an AJAX request to the server and initiate the script execution, then close the connection in the PHP script and continue processing on the server side. This way, the user's browser will no longer be waiting for a response from the server.

2. Use a background worker or queue system: Instead of executing the time-consuming task directly in the PHP script, you can offload it to a background worker or a queue system like RabbitMQ or Redis. The PHP script can add the task to the queue, close the connection with the browser, and the background worker can then handle the task independently.

3. Implement a cron job: If the task doesn't need to be executed immediately upon receiving the request, you can schedule it as a cron job. The PHP script can receive the request, trigger the cron job, and then close the connection with the browser. The cron job will execute the task at the specified time interval.

These alternatives may require additional setup and adjustments to your codebase, but they can allow you to achieve the desired outcome without modifying the php.ini file.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...