2011-09-23 34 views
1

我遇到了echo/print返回大量數據的問題。該響應被打破,如下:大的打印/回聲中斷http響應

  • 數據的端
  • 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; 

?> 

這裏發生了什麼?

其他人可以試試嗎?

回答

0

您可能會在服務器上激活自動輸出緩衝。如果緩衝區溢出,它只是開始將其餘的數據放出來。

請注意,諸如gzip壓縮之類的內容也會隱式地緩存輸出。如果是這種情況,在頭文件之後調用ob_end_flush()應該解決它。

+0

我試過添加ob_end_flush();在我的腳本結尾處,問題是一樣的。 –

+0

不是最後,在頭文件和'echo'語句之間。 – Rijk

+0

試過了,不幸的是同樣的問題 –

0

瀏覽器通常會限制您允許通過get變量的字符。 要解決此問題,您可以將64位字符串進行編碼,然後在收到響應後對其解碼。

我認爲有JavaScript base 64編碼庫可用。 像這樣: http://www.webtoolkit.info/javascript-base64.html

+0

嗨,我沒有通過一個get變量傳遞任何值。這個腳本實際上只是在瀏覽器中查看時出錯。 –