Aiutooo!
Nel seguente spezzone di codice, che dovrebbe fare una cosa semplicissima ovvero prelevare la homepage di Google, è venuto fuori un problema che gli altri giorni non c'era e non mi so spiegare il motivo, sto uscendo matto.
Codice PHP:
<?
function mylog($line)
{
$fp = fopen('log.txt', 'ab');
$t = date('[d/m/Y H:i:s] ').$line."\r\n";
fputs($fp, $t);
fclose($fp);
}
@unlink("log.txt");
mylog("Application started.");
$host = "www.google.it";
$target = "index.html";
$sk = fsockopen($host, 80, $errnum, $errstr, 180);
$dataout = '';
if(!is_resource($sk))
{
mylog("ERROR $errnum $errstr");
}
else
{
$br = "\n";
$headers= "GET /".$target." HTTP/1.0".$br;
$headers.="Accept: */*".$br;
$headers.="Accept-Language: it".$br;
$headers.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)".$br;
$headers.="Host: ".$host.$br;
$headers.="Proxy-Connection: Keep-Alive".$br.$br;
mylog($headers);
fputs($sk, $headers);
while(!feof($sk))
{
mylog('+');
$dataout.= fgets($sk, 2048);
mylog('-');
}
fclose($sk);
}
mylog($dataout);
mylog("Application terminated.");
?>
La funziona mylog() logga su file di testo una riga per volta. Lo script se mandato in esecuzione non finisce mai, rimane piantato su fgets(), manon subito, ad un certo punto, infatti il log.txt riporta:
codice:
[29/07/2004 15:00:38] Application started.
[29/07/2004 15:00:38] GET /index.html HTTP/1.0
Accept: */*
Accept-Language: it
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Host: www.google.it
Proxy-Connection: Keep-Alive
[29/07/2004 15:00:38] +
[29/07/2004 15:00:39] -
[29/07/2004 15:00:39] +
[29/07/2004 15:00:39] -
[29/07/2004 15:00:39] +
[29/07/2004 15:00:39] -
[29/07/2004 15:00:39] +
[29/07/2004 15:00:39] -
[29/07/2004 15:00:39] +
[29/07/2004 15:00:39] -
[29/07/2004 15:00:39] +
[29/07/2004 15:00:39] -
[29/07/2004 15:00:39] +
[29/07/2004 15:00:39] -
[29/07/2004 15:00:39] +
[29/07/2004 15:00:39] -
[29/07/2004 15:00:39] +
[29/07/2004 15:00:39] -
[29/07/2004 15:00:39] +
[29/07/2004 15:00:39] -
[29/07/2004 15:00:39] +
[29/07/2004 15:00:39] -
[29/07/2004 15:00:39] +
[29/07/2004 15:00:39] -
[29/07/2004 15:00:39] +
Forse il bug è anche in feof() che dovrebbe ritornare FALSE prima...
Dove sbaglio? Help!!!