dpacmittal Posted July 16, 2009 Share Posted July 16, 2009 I have a script which downloads several files from a remote server. When I clicked the stop button of browser, the loading stopped but the PHP file was still being executed on the server. I found out because it downloaded all the files for remote server. Isn't there a way to stop execution when browser is stopped. Quote Link to comment https://forums.phpfreaks.com/topic/166174-how-to-stop-execution-of-script-once-the-page-has-been-stopped-from-loading/ Share on other sites More sharing options...
ignace Posted July 16, 2009 Share Posted July 16, 2009 I always tought that when you pressed the stop button on your browser it also stopped the request/response cycle on the server. However this may be different for sockets and streams I am not entirely sure although php performs a garbage collection after the script is executed. I find this very interesting someone who can shed some more light on the subject? Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/166174-how-to-stop-execution-of-script-once-the-page-has-been-stopped-from-loading/#findComment-876367 Share on other sites More sharing options...
phporcaffeine Posted July 16, 2009 Share Posted July 16, 2009 When you hit the stop button on the browser it stops client-side activity and any subsequent requests made to the server by the browser. However, a user's browser based control (the stop button) cannot effect anything on the server. So if a user action stops the request from being sent, fine .... but the server is still going to finish what it was in the middle of doing. In your case, the only way to prevent any files from being downloaded would have been to stop the browser prior to it making the request to the server to begin the download process altogether. The request / response relationship between client browser and web server isn't a 'real-time' (notwithstanding RTSP) flow where if I stop the flow from one end it stops it everywhere, the relationship is more like one-sided ping-pong. Client requests and server responds. Quote Link to comment https://forums.phpfreaks.com/topic/166174-how-to-stop-execution-of-script-once-the-page-has-been-stopped-from-loading/#findComment-876377 Share on other sites More sharing options...
dpacmittal Posted July 16, 2009 Author Share Posted July 16, 2009 Thanks, that means it will always do what it is requested, no matter if you've stopped the page from loading. Thats cool and sometimes troublesome. What if I make an infinite loop on a page and load it in the browser? Will it run forever until the server crashes? Quote Link to comment https://forums.phpfreaks.com/topic/166174-how-to-stop-execution-of-script-once-the-page-has-been-stopped-from-loading/#findComment-876385 Share on other sites More sharing options...
phporcaffeine Posted July 16, 2009 Share Posted July 16, 2009 Sure .. you can make a php loop ... it will generally time out before it crashes the server though ... there are many stop-gaps to prevent that. PHP will eat up it's memory_limit or script execution time before that happens. You can certainly hose the Apache child process enough to where you need to restart Apache but crashing the actual server just from a php loop may be a stretch unless it is a winblows server. Now if the loop started other process on the server via exec() or system() ... that may be a different story. Quote Link to comment https://forums.phpfreaks.com/topic/166174-how-to-stop-execution-of-script-once-the-page-has-been-stopped-from-loading/#findComment-876393 Share on other sites More sharing options...
dpacmittal Posted July 16, 2009 Author Share Posted July 16, 2009 Sure .. you can make a php loop ... it will generally time out before it crashes the server though ... there are many stop-gaps to prevent that. PHP will eat up it's memory_limit or script execution time before that happens. You can certainly hose the Apache child process enough to where you need to restart Apache but crashing the actual server just from a php loop may be a stretch unless it is a winblows server. Now if the loop started other process on the server via exec() or system() ... that may be a different story. Thanks for information. The things you mentioned which would stop the execution of PHP script was already taken care of with this: set_time_limit(0); ini_set(”memory_limit”,”100M”); lol.. so naturally, the script would run forever if put in an infinite loop. Quote Link to comment https://forums.phpfreaks.com/topic/166174-how-to-stop-execution-of-script-once-the-page-has-been-stopped-from-loading/#findComment-876399 Share on other sites More sharing options...
trq Posted July 16, 2009 Share Posted July 16, 2009 Take a look at connection handling. Quote Link to comment https://forums.phpfreaks.com/topic/166174-how-to-stop-execution-of-script-once-the-page-has-been-stopped-from-loading/#findComment-876451 Share on other sites More sharing options...
dpacmittal Posted July 16, 2009 Author Share Posted July 16, 2009 Take a look at connection handling. Well, thanks for the link. But I haven't used ignore_user_abort() but still it doesn't stop. Is it because PHP.ini is modified that way? I've checked my config using phpinfo(). It shows ignore_user_abort off. So, can anyone solve this mystery. Quote Link to comment https://forums.phpfreaks.com/topic/166174-how-to-stop-execution-of-script-once-the-page-has-been-stopped-from-loading/#findComment-876501 Share on other sites More sharing options...
phporcaffeine Posted July 16, 2009 Share Posted July 16, 2009 Here is an example of using ignore_user_abort() <?php ignore_user_abort(true); set_time_limit(0); echo 'Testing connection handling in PHP'; while(1) { if(connection_status() != CONNECTION_NORMAL) { break; } sleep(10); } ?> Now, if encountered, the script will stop ... but only upon the next attempted iteration of the loop (when it encounters the instruction to check the connection again) ... it will however continue with the rest of the code in the current iteration ... which in the original posters case was downloading a file (which I imagine you are using PHP to stream it to the header). Quote Link to comment https://forums.phpfreaks.com/topic/166174-how-to-stop-execution-of-script-once-the-page-has-been-stopped-from-loading/#findComment-876506 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.