//INIZIO CONTROLLO timeout
$contatore_per_timeout = 1;
$stop_ogni = 30;

while .......
if($contatore_per_timeout == $stop_ogni)
{
set_time_limit(30);
sleep(1);
$contatore_per_timeout = 0;
}
$contatore_per_timeout++
//FINE CONTROLLO timeout