SupunR
SupunR

Reputation: 21

how to exit php code after a few seconds?

I want to exit my php code after 10 seconds. Is there any possible way to do that? I have already found this code but it's not working.

set_time_limit(10);  *//this line is blocked from the server*
ini_set('max_execution_time', 10);

Upvotes: 2

Views: 3174

Answers (2)

Snoopy
Snoopy

Reputation: 25

I think that this code may help you:

$starttime = time();

do{
    //make something
}
while ((time() - $starttime)<5); //stop with 5 seconds

Upvotes: 2

bfday
bfday

Reputation: 155

Did you put set_time_limit at the begining of the script? if not - do it, because if you look at official documentation you will see next:

When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.

Also check it's return value: "Returns TRUE on success, or FALSE on failure."

PS: "The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real."

Does it help?

All written above you can find in http://php.net/manual/en/function.set-time-limit.php

Upvotes: 5

Related Questions