PHP execute script in real time

Discussion in 'PHP' started by DanielRosca, Jan 3, 2009.

  1. #1
    Hello everybody,

    I have got a question. I have a script in php and I want to execute something at a fixed time in the future, not by cron server.
    Let me explain more. I would create a car in a game, but for this I have to wait around 10 minutes. Ok, now I used time() and I set it up like this: time()+600. I am testing if time() is equal or greater than that time stored in database. If I do like this I have to refresh the page after 10 minutes to add the car. I don't want to do this anymore, I want to do it automatically after 10 minutes.
    What should I use for this?

    Thanks,
    Daniel Rosca
     
    DanielRosca, Jan 3, 2009 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Cron is by far the preferred way, just run a script every x often, which then runs the required queries, really not that hard.

    Thinking of some alternatives could be... (wouldn't recommend.)

    
    /* At top of script */
    ob_implicit_flush(true);
    ignore_user_abort();
    set_time_limit(0);
    
    /** Script here **/
    
    /* At end */
    sleep(600);
    do_query($query);
    exit(); // Could probably remove this, depends on your script.
    
    PHP:
    Still, I wouldn't recommend it, but there's no reason why it shouldn't work.
     
    Danltn, Jan 3, 2009 IP
  3. DanielRosca

    DanielRosca Active Member

    Messages:
    158
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #3
    I heard something about javascript counter, but I really don't know something about javascript.
     
    DanielRosca, Jan 3, 2009 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Could just use a javascript refresh set to 600 seconds, pretty poor substitute for cron though.
     
    Danltn, Jan 3, 2009 IP
  5. manjifera

    manjifera Well-Known Member

    Messages:
    232
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    118
    #5
    if you want to refresh the page after 10 minutes then use this html inside php

    add this code in u r php file in header!
    <?php 
    echo "<meta http-equiv=\"refresh\" content=\"600\" />";
    //600 for 10 minutes 
    ?>
    
    PHP:
     
    manjifera, Jan 3, 2009 IP
  6. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #6
    Do you want to run it once after 10 minutes or every 10 minutes?
     
    Kaizoku, Jan 3, 2009 IP
  7. DanielRosca

    DanielRosca Active Member

    Messages:
    158
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #7
    I don't have a fixed time. I solved the problem with JS.
     
    DanielRosca, Jan 3, 2009 IP