How to prevent maximum execution time on my code

I have problem and i need some help,I already change the execution time from

max_execution_time = 300 

to

max_execution_time = 300

but still I always get Fatal error: Maximum execution time of 300 seconds exceeded

can anyone help me on this to make my code works,so that it will run smoothly.

Thank you in advance.


<?php

$address = 'xxx.xxx.xx.xx';
$port = xxxx;

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);


socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);


socket_bind($sock, $address, $port);


socket_listen($sock);



$clients = array($sock);

while (true) {
    $file = fopen('txt.log','a');

    $read = $clients;
    $write = NULL;
    $except = NULL;
    $tv_sec = 0;

    if (socket_select($read, $write , $except,  $tv_sec) < 1)
        continue;

    // checking client
    echo "now connecting...\\r\
";
    if (in_array($sock, $read)) {

        $clients[] = $newsock = socket_accept($sock);

        $key = array_search($sock, $read);
        unset($read[$key]);

    }


    foreach ($read as $read_sock) {


        $data = @socket_read($read_sock, 1024, PHP_NORMAL_READ);


        if ($data === false) {

            $key = array_search($read_sock, $clients);
            unset($clients[$key]);
            echo "client disconnected.\
";
            echo  "Remaining ".(count($clients) - 1)."client(s) connected\\r\
";
            continue;
        }


        $data = trim($data);


        if (!empty($data)) {
            echo("Returning stripped input\
");

            fwrite($file,$data."\
");




        }

    } // end of reading foreach

    fclose($file);
}


socket_close($sock);

?>

In general, you shouldn’t need to modify max_execution_time. In most cases, if you find that your script is exceeding the default execution time, you need to take a second look at your script.

A quick look at your code though, shows you have an infinite loop. Your script, as is, will never end.


while (true) {

You either need to change “true” to something that will eventually evaluate to false, or add a “break” inside the loop where appropriate to break out of the loop. I see you have a couple of “continue” statements in there but continue is different from break. While break will end the loop, continue just tells PHP to restart the loop at the beginning.

The above is good advice, and the while loop is causing the timeout, unless that text file has a ton of data in it.

Not only that, but you never actually changed the execution time…

@kduv, so you mean to change the continue to break ?by the way it is 120 then i change it to 300…

Looks like you’d change the 2nd instance of continue to break; leave the first one as is

I’d leave the max_execution_time at its server default. It’s there to protect the server from things like infinite loops. PHP usually defaults to 30 seconds I think. That timeout isn’t the time it takes to load an entire page, it’s just the time it takes to run the PHP. Even huge PHP apps rarely need more than 30 seconds. If your code needs to run much longer than that, then you may want to look at something that’s more designed for longer running processes. Maybe Python, C/++, Ruby, or something else.

you mean to this



        if ($data === false) {

            $key = array_search($read_sock, $clients);
            unset($clients[$key]);
            echo "client disconnected.\
";
            echo  "Remaining ".(count($clients) - 1)."client(s) connected\\r\
";
            continue;
        } 

to this



        if ($data === false) {

            $key = array_search($read_sock, $clients);
            unset($clients[$key]);
            echo "client disconnected.\
";
            echo  "Remaining ".(count($clients) - 1)."client(s) connected\\r\
";
            break;
        } 


Yeah I think that’s the one he’s suggesting.

ok i will try hi suggestion

Yes, actually, I’d change it to break 2; that way you are escaping the while loop as well…which I assume you have fixed already

It doesn’t work…still excedeed

It’s going to keep doing that until you fix the while loop

Try adding error_reporting and display errors then break points to see what is happening



<?php 
error_reporting(-1); 
ini_set('display_errors', 1);


function dd( $val ) // debug
{
  echo '<pre>';
    print_r( $val );
  echo '</pre>';
  die;
}


$address = '123.453.78.90'; 
$port    = 8080; 




// change the $address variable and move down the script to see what is happening
echo '<br />line: ' .__LINE__ ; dd($address); 



$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 


socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1); 


socket_bind($sock, $address, $port); 


socket_listen($sock); 



$clients = array($sock); 

while (true) { 
    $file = fopen('txt.log','a'); 

    $read = $clients; 
    $write = NULL; 
    $except = NULL; 
    $tv_sec = 0; 

    if (socket_select($read, $write , $except,  $tv_sec) < 1) 
        continue; 



HI John_Betong, You mean to move this down

echo '<br />line: ' .__LINE__ ; dd($address); 

In each line of my code ?

example if i want to debug in the if statement , I will put the function


  if (socket_select($read, $write , $except,  $tv_sec) < 1) {
         echo '<br />line: ' .__LINE__ ; dd($read); //is this what  you mean ?..if no error here i will move down to my code 
        continue;
}
 

Yes and while the browser is still open it will show the line number and the contents of the variable.

Keep the browser open, move the line down to the next point that you want to check then refresh the browser. It will then stop on the next breakpoint.

If you are concerned with the time then you could modify the function to include the time elapsed:


$time_start = microtime();

function dd( $val ) // debug
{
  echo '<pre>';
    print_r( $val );

    $time_stop = microtime() - $time_start;
    echo '<br />millisecs == ' . $time_stop; 

  echo '</pre>';
  die;
}


e

Ok thank you for this ,.I will let you know.if it’s working

@John Betong,

why is it

Notice: Undefined variable: time_start

but time_start is declared before the function dd.

Whoops, $time_start is not global.

Either set $time_start as global or pass $time_start to the function:


global $time_start;
$time_start = microtime();

function dd( $val, $time_start ) // debug 
{ 
  echo '<pre>'; 
    print_r( $val ); 

    $time_stop = microtime() - $time_start; 
    echo '<br />millisecs == ' . $time_stop;  

  echo '</pre>'; 
  die; 
}  


 echo '<br />line: ' .__LINE__ ; dd($read, $time_start); // move down


why is it that if i run this to a batch file the code will not through maximum execution time limit exceeded and i think it is working fine i am observing 5 hours with the script and it did not stop or through error. ,but if I run this to browser after 5 mins or less it will through fatal error maximum execution time exceeded…

This looks like a batch process better served by being run from the command line. If so, invoke the script from the prompt using “php scriptname.php”

The PHP CLI parser doesn’t have a maximum execution time. It will run until it concludes or you hit CTRL-C to kill the process.