I have a PHP script that does this:
$sec = 1;
$id = 1;
while ($sec<20) {
sleep(1);
mysql_query("update test set sec = $sec where id = $id");
$sec++;
}
Whenever I load the script in my browser, it will keep on going even if I closed the browser window after 5 seconds. How can I change this so when the browser window is closed, the script will exit?
You can’t. Once the HTTP request has been sent off to the server, the PHP script will continue to execute to it’s end even if the client has disconnected.
There might be some cases where a script will exit while printing things, but seeing as you’re not printing anything it won’t exit.