Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can closing the browser terminate the PHP script on the server?

Tags:

Say a user clicked a button, which resulted in a jquery ajax request being sent to my server.

The server begins a complicated process in response to the ajax request. Lets say that process takes 5 minutes to finish.

In the meantime the user gets bored and closes his browser window.

Will the script on the server continue its processing until it finishes on its normal time, or will it stop?

Same thing if the user visits a url, e.g example.com/process.php?data=xxx. This starts process.php which begins to process the data, and will take 5 mins to finish processing.

Will this processing always continue on, or if the user closes the browser, will it stop?

(I'm asking because I'm concerned about the process being left half finished and resulting in corrupt data).

like image 966
Ali Avatar asked Jul 06 '12 10:07

Ali


People also ask

Does PHP run in browser or server?

Chapter 1. PHP: What, Why, and Where? PHP is ultimately just text that is taken by your web server and turned into a set of commands and information for your web browser. And because you're just working in text, there's not a lot you have to do to get going as a PHP programmer.

How do you kill a PHP script?

You can use pkill -9 php or if pkill itsn't present you can use ps -efw | grep php | grep -v grep | awk '{print $2}' | xargs kill to kill all php processes.

Are PHP scripts executed on server?

PHP is a server side scripting language. This means that it is executed on the server. The client applications do not need to have PHP installed.

Why PHP script is not running in browser?

PHP is not intended for execution in a browser. It is for web servers to execute, or other preprocessing on the PHP-installed computer. PHP runs in several incarnations when installed on a computer: from the command line.


2 Answers

PHP won't notice that the browser closed the connection unless it tries to output something (e.g. echo). If it fails to output something, the script will be terminated unless ignore_user_abort is On.

So the script will terminate only if the following conditions are met:

  • the script attempts to output something (because the script won't notice that the user aborted the connection until then)
  • AND php's ignore_user_abort setting is off (if the setting is off, php will terminate the script if fails to output something)

You can avoid the script from terminating by enabling ignore_user_abort.

You can use connection_aborted() at anytime to check is the browser aborted the request.

A script can terminate unexpectedly for other reasons (e.g. max execution time, exception, etc); so you should make use of transactions, so that half-finished changes are canceled if the script terminates abnormally.

like image 194
Arnaud Le Blanc Avatar answered Oct 07 '22 23:10

Arnaud Le Blanc


See: http://php.net/manual/en/function.ignore-user-abort.php. It depends on the ignore_user_abort setting and a few other conditions.

like image 30
D-Rock Avatar answered Oct 08 '22 01:10

D-Rock