Home » Php » php – Why I get "ERR_RESPONSE_HEADERS_TOO_BIG" on chrome?

php – Why I get "ERR_RESPONSE_HEADERS_TOO_BIG" on chrome?

Posted by: admin July 12, 2020 Leave a comment

Questions:

The following code is executed correctly on firefox, but not on Chrome. The later always displays “ERR_RESPONSE_HEADERS_TOO_BIG”. This error occurs when I iterate through a folder containing more than 10000 items (files) – it must be long task.

Please can anyone explain me how to avoid this error? The error disappears if I “echo”-ing something just after ob_end_clean();

<?php

ini_set('max_execution_time', 600);

function FileItemsCount($it, &$count_ref)
{ 
   foreach ($it as $file)
   {
        $count_ref += 1;

ob_start();
        session_start();
        $_SESSION['progress'] = $count_ref;
        session_write_close(); 
ob_end_clean() ;

        $is_folder = $it->hasChildren();
        if ($is_folder)
        {
           FileItemsCount($it->getChildren(), $count_ref);
        }
    }
}

$dir = "C:/Users/sstefanov/xampp";

$it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);

$count = 0;
FileItemsCount($it, $count);


echo $count;

?>
How to&Answers:

Some necroposting for those, who will search an answer 🙂

I tried to do the similar thing – to unblock parallel ajax requests, one of which called for long operation and other for checking progress.
I also started the session each time to write new value and stopped it right after. And I also got ERR_RESPONSE_HEADERS_TOO_BIG error in Chrome.

The reason was that PHP sent Set-Cookie header with each session start in my case. And when long ajax request was finishing, server sent all Set-Cookie headers to the browser.

To fix this I removed the Set-Cookie headers by calling
header_remove('Set-Cookie'); before the output.

Answer:

It’s not the case in this questions, but I thought that it would be useful to share that this error could also arise if too much data is being printed/echoed by the server (e.g. for debugging purposes)