Script still running after page is stopped

Discussion in 'PHP' started by AHA7, Oct 24, 2007.

  1. #1
    I am testing a PHP script on my server and I noticed that when I visit the script page in my browser and click "stop" before the script has finished executing then the browser closes the connection with the server and outputs its own "page not found" error page, but even after the browser has closed the connection the script keep running on my server until it is completely executed.

    For a quick test, I have made a test script that sleeps for 10 seconds and then stores some data in a database, now I visited that script in my browser and quickly clicked "stop" so that the browser gave me a "page not found" error but nevertheless the script kept running on my server 'til its end and the data was stored in the database indicating that the statements after the 10 sec sleep has been executed!

    Is there anyway I can make my scripts exit immediately when someone clicks "stop" (ie the user browser cuts the connection) even before it's finished executing?
     
    AHA7, Oct 24, 2007 IP
  2. tarponkeith

    tarponkeith Well-Known Member

    Messages:
    4,758
    Likes Received:
    279
    Best Answers:
    0
    Trophy Points:
    180
    #2
    Hmmm... There might be a way to use the php command:

     
    tarponkeith, Oct 24, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    Perhaps something along the lines of:
    
    
    while (!connection_aborted())
    {
        // Whatever
    
        flush();
    }
    
    PHP:
     
    nico_swd, Oct 24, 2007 IP
    AHA7 likes this.
  4. tarponkeith

    tarponkeith Well-Known Member

    Messages:
    4,758
    Likes Received:
    279
    Best Answers:
    0
    Trophy Points:
    180
    #4
    I'd listen to nico instead of me... I'm an ASP guy trying to make the transition to PHP, while nico has proven his PHP-ability time and time again...
     
    tarponkeith, Oct 24, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Lol, thanks. It was just a wild guess too though. :)
     
    nico_swd, Oct 24, 2007 IP
  6. craze3

    craze3 Well-Known Member

    Messages:
    454
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Two PHP functions that may assist you in this:
    exit();
    Code (markup):
    and
    mysql_close();
    Code (markup):
     
    craze3, Oct 24, 2007 IP