Jump to content

How to stop execution of script once the page has been stopped from loading


dpacmittal

Recommended Posts

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.