user2019515
user2019515

Reputation: 4503

PHP Start script where it timed out

I'm currently building a web crawler but to start I would like to test it on my shared webhosting, obviously they do not allow set_time_limit so I can't make sure the script keeps running for longer than 30 seconds.

What would be the best way to start the PHP script the next time where it timed out before? I was thinking about saving the last crawled URL in a file but are there any other options?

Upvotes: 1

Views: 208

Answers (1)

Rowland Rose
Rowland Rose

Reputation: 69

Edit: ScallioXTX is right, you can't use variables as a goto label. You could get around that with a very large if statement, but at this point I'd say it's best to not use goto at all. Here is an alternative method:

<?php

    // Load label number from database or text file into $label_num
    
    if($label_num <= 1) {
        // Do stuff
    }
    
    if($label_num <= 2) {
        // Do more stuff
    }
    
    // ...

?>

Old (incorrect) method:

You can of course use goto: https://www.php.net/goto

I would only use this as a temporary measure until you get better hosting.

Here's what I would do:

  1. At various locations throughout your code, write a label (ex. count1:, count2:, etc.)
  2. At each location where you added a label, write that label name to a database or text file
  3. At the beginning of your script, load that value and jump to the specified label

Example:

<?php

    // Load label number from database or text file into $label_num
    
    if($label_num) {
        goto $label_num;
    }
     
    count1:
    // Do stuff
    
    count2:
    // Do more stuff
    
    // ...

?>

Upvotes: 1

Related Questions