diff --git a/ChromePhp.php b/ChromePhp.php index ce4bf86..382a435 100755 --- a/ChromePhp.php +++ b/ChromePhp.php @@ -78,6 +78,11 @@ class ChromePhp */ const TABLE = 'table'; + /** + * @var int + */ + const HTTPD_HEADER_LIMIT = 8192; // 8Kb - Default for most HTTPD Servers + /** * @var int */ @@ -377,7 +382,35 @@ protected function _addRow(array $logs, $backtrace, $type) protected function _writeHeader($data) { - header(self::HEADER_NAME . ': ' . $this->_encode($data)); + $header = self::HEADER_NAME . ': ' . $this->_encode($data); + // Most HTTPD servers have a default header line length limit of 8kb, must test to avoid 500 Internal Server Error. + if (strlen($header) > self::HTTPD_HEADER_LIMIT) { + $data['rows'] = []; + $data['rows'][] = [ + [ + 'ChromePHP Error: The HTML header will surpass the limit of ' . + $this->_formatSize(self::HTTPD_HEADER_LIMIT) . ' (' . $this->_formatSize(strlen($header)) . + ') - You can increase the HTTPD_HEADER_LIMIT on ChromePHP class, according to your Apache ' . + 'LimitRequestFieldsize directive' + ], '', self::ERROR + ]; + $header = self::HEADER_NAME . ': ' . $this->_encode($data); + } + header($header); + } + + protected function _formatSize($arg) { + if ($arg > 0) { + $j = 0; + $ext = ["bytes","Kb","Mb","Gb","Tb"]; + while ($arg >= pow(1024, $j)) ++$j; { + $arg = (round($arg / pow(1024, $j - 1) * 100) / 100).($ext[$j - 1]); + } + + return $arg; + } + + return "0Kb"; } /**