php函数stream_set_timeout不能像我期望的那样工作

时间:2016-02-18 14:29:38

标签: php

我是每个人,

我正在尝试使这段代码正常工作,因为我将fread函数的超时设置为10(fread只读取2个caracteres)为什么它会返回“Time out!” ?

以下是代码:

<?p
 $url = "127.0.0.1:8081/SQLi/cible.php?id=sam";


$fp = fsockopen($url);


if (!$fp) {
          echo "Can't open\n";
     } else {


  stream_set_timeout($fp, 10);  // 10 seconds for fread to be executed

  $res = fread($fp, 2);         // read 2 caracteres of $fp

  $info = stream_get_meta_data($fp);


  fclose($fp);
  if ($info['timed_out']) {
     echo 'Timed out !';
  } else {
     echo $res;
  }

}

?>

1 个答案:

答案 0 :(得分:0)

好吧,我刚才意识到我从未正确回答过这篇文章,所以要完成Gerald所说的,fsockopen只是在URL上打开一个套接字,但它没有对应于GET请求,所以我不得不手动对服务器执行GET请求,以便后者可以回答我...

以下是更新代码:

<?php

$url = "www.google.fr";


$fp = fsockopen($url,80,$errno,$errstr,30);


if (!$fp) {


  echo "Can't open\n";
     } 

else {

  $out = "GET / HTTP/1.1\r\n";
  $out .= "Host: www.google.fr\r\n";
  $out .= "Connection: Close\r\n\r\n";

  fwrite($fp, $out);

  stream_set_timeout($fp, 10);  // 10 seconds for fread to be executed

  $res = fread($fp, 200);         // read 2 caracteres of $fp

  $info = stream_get_meta_data($fp);


  fclose($fp);
  if ($info['timed_out']) {
     echo 'Timed out !';
  } else {
     echo $res;
  }

}

?>