Results 1 to 7 of 7

Thread: Looping And User Aborting

  1. #1
    Join Date
    Jul 2011
    Posts
    56
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Looping And User Aborting

    I am trying to create a script that will keep checking the time and if it's the desired time lets say 17:01 o'clock, it will commit the action. Once it's not that time, the scripts stop and goes back to checking the time. Please set me on the right path because my script:
    PHP Code:
    <?php
    // Ignore user aborts and allow the script
    // to run forever
    ignore_user_abort(true);
    set_time_limit(0);
    $i=5;
    $time=date("Gi");
    while(
    $i==5){
    if (
    $time=="1701") {
        
    $myFile 'users/Guy/test.txt';
    $fh fopen($myFile'w') or die("<div class='errorbox' id='errorbox'>Oops! Something happened!</div>");
    $stringData "Awesome\n";
    fwrite($fh$stringData);
    $stringData date("l")." @ ".date("g").":".date("i").":".date("s")."\n";
    fwrite($fh$stringData);
    $stringData $_SESSION['username'];
    fwrite($fh$stringData);
    fclose($fh); 


    }
    }


    ?>
    And it doesn't seem to do that. I would rather not use cron jobs if that's possible!


    Thanks!
    Last edited by jscheuer1; 05-08-2012 at 01:48 AM. Reason: Format

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    most web hosts will kill scripts that try to keep running indefinitely.

    honestly, this is a very bad approach. Even if your host allowed the process to keep running, without some serious precautions, you could very quickly have too many runaway processes (e.g., someone sees no response and tries refreshing the page a few times) and literally crash the server.

    a cron job is vastly more appropriate for this sort of task. Is there any reason you want to avoid them?

  3. #3
    Join Date
    Jul 2011
    Posts
    56
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default

    The Cron Job seems to be too messy for me unless you know a simpler way to do it. I thought I heard that you could do that using the userabort. Thanks!

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    In theory, but this is dangerous because it will not be stoppable any more. Include a safety mechanism like a value in the database or just checking whether a file named "stop.txt" exists on the server.
    As traq says, it may also bother your host. Be careful with that.

    If you do want to simulate a cron job, then make an infinite loop and add sleep(30) (or however many seconds you want between each iteration). Having it run all the time will be terrible for your server. Give it a break sometimes. Realistically you probably don't want less than 5 minute delay between loops.

    Note also that if the user is gone the error won't output-- you need to do all of this in a text file, including errors (unless you don't care about them).


    In the end, yes, use a cron job. They can be a little difficult, but usually with certain hosts-- probably the same ones that would get annoyed about your infinite looping script
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by M2com View Post
    The Cron Job seems to be too messy for me unless you know a simpler way to do it.
    What do you find "messy" or "complicated" about it?
    Your script could be used as a cron almost as it is written.
    most hosts (e.g., any host using cPanel) make it very easy to schedule a job.

    Quote Originally Posted by djr33 View Post
    certain hosts-- probably the same ones that would get annoyed about your infinite looping script
    honestly, I've never found a (shared) host that doesn't auto-kill scripts that run too long (sometimes, regardless of whether they're actually "running away" or not). Most will contact you and demand you fix the situation. A few will actually cancel your service if you don't stop.

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Hm, I've never actually tried that. So I wouldn't be surprised if it is all of them.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    For most hosts, this is just based on reading their terms of service.
    I've actually only had the experience twice (once my own script, once fixing someone else's).

  8. The Following User Says Thank You to traq For This Useful Post:

    djr33 (05-09-2012)

Bookmarks

Posting Permissions

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