大打印/回声中断http响应

时间:2011-09-23 08:50:50

标签: php apache http http-headers wamp

我遇到了返回大量数据的echo / print问题。答案被打破,如下:

  • 数据结束
  • 在身体中打印的http响应标题
  • 数据开始

我正在运行以下脚本到我的浏览器来复制问题:

<?php

// Make a large array of strings
for($i=0;$i<10000;$i++)
{        
  $arr[] = "testing this string becuase it is must longer than all the rest to see if we can replicate the problem. testing this string becuase it is must longer than all the rest to see if we can replicate the problem. testing this string becuase it is must longer than all the rest to see if we can replicate the problem.";
}

// Create one large string from array
$var = implode("-",$arr);

// Set HTTP headers to ensure we are not 'chunking' response
header('Content-Length: '.strlen($var)); 
header('Content-type: text/html');

// Print response
echo $var;

?>

这里发生了什么?

其他人可以试试吗?

2 个答案:

答案 0 :(得分:0)

您可能在服务器上激活了自动输出缓冲。如果缓冲区溢出,它只是开始对其余的数据进行扩展。

请注意,像gzip压缩这样的东西也会隐式缓冲输出。如果是这种情况,标题后的ob_end_flush()调用应解决它。

答案 1 :(得分:0)

浏览器通常会限制您允许通过get变量传递的字符。 要解决这个问题,你可以对64字符串进行编码,然后在收到响应后对其进行解码。

我认为有javascript base 64编码库可用。 像这个: http://www.webtoolkit.info/javascript-base64.html