Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2012
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ignore_user_abort()

    Could you give me an example with ignore_user_abort() ?
    Thank you!

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,324
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    PHP Code:
    <?php
    ignore_user_abort
    ();

    while (
    $Something_is_true)
       {
       
    //Do something here - check a socket connection, monitor a database etc.
       
    }
    ?>

    That will keep cycling until your condition becomes false. The user can call the page via their browser, click stop and the script will continue to run. It will however stop after the max_execution_time setting (often 30 - 60 seconds) so you may need to use set_time_limit(0) to override that (but some hosts disable that).

    You use this function at your own risk and peril. Its dangerous in the wrong unenlightened hands because if you don't put in a check that will terminate the script, it can carry on running and eat up all the CPU resources of your server. If you're on a shared machine then your host will almost certainly shut you down. You MUST write in a clause that will stop the script - EG just checking if a file called stop.txt exists (or to avoid any path issues and the file never being fouind, check a database table or session instead).

    Also in your loop be sure to use a delay such as sleep(1) to make the script sleep for a second. Why? Because if you don't the loop will try to run as fast as the processor will allow - eating the CPU usage again as described above. It will take over the CPU and lock out other processes that need it. You'll get shut down fast.
    Last edited by tangoforce; 01-20-2013 at 01:54 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    New Coder
    Join Date
    Feb 2012
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have read the php manual, but I have some problems. For exemple in this script from here
    http://php.net/manual/en/function.ig...ser-abort.php:
    Code:
    <?php
    // Ignore user aborts and allow the script
    // to run forever
    ignore_user_abort(true);
    set_time_limit(0);
    
    echo 'Testing connection handling in PHP';
    
    // Run a pointless loop that sometime
    // hopefully will make us click away from
    // page or click the "Stop" button.
    while(1)
    {
    // Did the connection fail?
    if(connection_status() != CONNECTION_NORMAL)
    {
         break;
    }
    
    // Sleep for 10 seconds
    sleep(10);
    }
    
    // If this is reached, then the 'break'
    // was triggered from inside the while loop
    
    // So here we can log, or perform any other tasks
    // we need without actually being dependent on the
    // browser.
    ?>
    If I run this script I can't see the message 'Testing connection handling in PHP'. in fact the script is running without ptrint any message. You can see here:
    http://ionutspot.uhostall.com/1.php

    And if i delete
    Code:
    ignore_user_abort(true);
    set_time_limit(0);
    the script will make the same thing like on the page http://ionutspot.uhostall.com/1.php

    So i hope that you understood me, i can't understand how could i use ignore_user_abort....

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,324
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    This is a complex issue.

    I will answer your points below in RED.

    Quote Originally Posted by milanello72 View Post
    I have read the php manual, but I have some problems. For exemple in this script from here
    http://php.net/manual/en/function.ig...ser-abort.php:
    Code:
    <?php
    // Ignore user aborts and allow the script
    // to run forever
    ignore_user_abort(true);
    set_time_limit(0);
    
    echo 'Testing connection handling in PHP';
    You will not see this printed to your browser unless the script ends while your browser is still connected. You would be better putting this comment into a logfile or database table. You could attempt to flush() but this rarely works.
    
    // Run a pointless loop that sometime
    // hopefully will make us click away from
    // page or click the "Stop" button.
    while(1)
    {
    // Did the connection fail?
    if(connection_status() != CONNECTION_NORMAL)
    {
    You need to log this action so that you know the script has stopped. Otherwise you could end up with anything from 10 - 100 versions of this script running and you wouldn't even realise.
         break;
    }
    
    // Sleep for 10 seconds
    sleep(10);
    Slight overkill at 10 seconds - you'll be waiting forever to know what the script is doing. Bring this down to 1 second - much easier for debugging.
    }
    
    // If this is reached, then the 'break'
    // was triggered from inside the while loop
    
    // So here we can log, or perform any other tasks
    // we need without actually being dependent on the
    // browser.
    You could also use register_shutdown_function instead.
    ?>
    If I run this script I can't see the message 'Testing connection handling in PHP'. in fact the script is running without ptrint any message. You can see here:
    http://ionutspot.uhostall.com/1.php

    You won't see anything because the script is still running. Therefore the webserver doesn't know how much or what type the output content is and won't flush the reply headers. Even if you force a flush the browser may not consider an incomplete page worth renderring / displaying while its still connected because it will expect the server to send more data.

    And if i delete
    Code:
    ignore_user_abort(true);
    set_time_limit(0);
    the script will make the same thing like on the page http://ionutspot.uhostall.com/1.php

    As above. While in a loop you can forget about getting any reliable form of output displayed in your browser.

    So i hope that you understood me, i can't understand how could i use ignore_user_abort....
    You can only really use ignore_user_abort on scripts that do things on the server side that your visitor doesn't need to see. For instance you might want a visitor to trigger a maintenance script to optimize your database.

    It does nothing for your visitor. It simply tells php that the script can carry on running after the visitors browser has closed the connection.
    Last edited by tangoforce; 01-20-2013 at 05:11 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •