Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid PHP execution time limit

I need to create a script in PHP language which performs permutation of numbers. But PHP has an execution time limit set to 60 seconds. How can I run the script so that if you need to run more than 60 sesunde, not been interrupted by the server. I know I can change the maximum execution time limit in php, but I want to hear another version that does not require to know in advance the execution time of a script.

A friend suggested me to sign in and log out frequently from the server, but I have no idea how to do this.

Any advice is welcome. An example code would be useful.

Thanks.

First I need to enter a number, lets say 25. After this the script is launch and it need to do the following: for every number <= than 25 it will create a file with the numbers generated at the current stage; for the next number it will open the previuos created file, and will create another file base on the lines of the opened file and so on. Because this take to long, I need to avoid the script beeing intrerupted by the server.

like image 801
Emanuel Avatar asked Jan 27 '10 14:01

Emanuel


Video Answer


1 Answers

@emanuel:

I guess when your friend told you "A friend suggested me to sign in and log out frequently from the server, but I have no idea how to do this.", he/she must have meant "Split your script computation into x pieces of work and run it separately"

For example with this script you can execute it 150 times to achieve a 150! (factorising) and show the result:

// script name: calc.php

<?php

 session_start();

 if(!isset($_SESSION['times'])){

    $_SESSION['times'] = 1;

    $_SESSION['result']  = 0;

 }elseif($_SESSION['times'] < 150){

    $_SESSION['times']++;

    $_SESSION['result'] = $_SESSION['result'] * $_SESSION['times'];

    header('Location: calc.php');

 }elseif($_SESSION['times'] == 150){

    echo "The Result is: " . $_SESSION['result'];

    die();

 }

?>

BTW (@Davmuz), you can only use set_time_limit() function on Apache servers, it's not a valid function on Microsoft IIS servers.

like image 138
turik Avatar answered Oct 08 '22 03:10

turik