Alex Rivera | Logout

How to execute a large PHP Script?

Asked 2010-05-15T15:50:40.850
19

Well basically I may want to execute a script that may take as much as 1 hours as well.

What I really want to do is Send SMS to my users using a third party API. So its basically like I supply my script with an array of phone numbers and fire the method to send SMS.

However assuming it take 5 seconds to send 1 SMS and I want to send 1000 SMS which is roughly 1 - 2 hours. I can't use set_time_limit() because I am on shared host.

One way to do this is store numbers in a session and execute each SMS and use javascript to refresh that page until end. This way I need to keep my browser open and the execution will stop if my Internet Connection is disconnected.

So, Is there any better way to do this ?

Hope I am clear enough to explain what I want? I want to execute a large script that may take hours to execute without getting timeout.

Edit
Report

2 Answers

3

It's not the best options to use set_time_limit(0), because that'd means it'll run indefinitely even if you have a bug and your script enters an infinite loop.

Instead, if you estimate each SMS is going to take 5 seconds, use this approach:

while( $there_are_more_sms_to_be_sent ){
  set_time_limit(30); // enough spare time, just in case.

  // Do your sending, blah blah
}

That way, the time limit will be sequentially updated to 30 seconds. Of course you might have the infinite loop problem with that single while, but if you have other calls inside that while that limit will prevent those calls to be to blame.

answered 2010-05-15T16:09:33.820
0

You can or you can't use set_time_limit()?

If you can.. Use it:

<?php
// Runs forever and ever...
set_time_limit(-1);
?>
answered 2010-05-15T15:55:17.530

Your Answer