Home > Article > Backend Development > firefox prompt: Content encoding error cannot display the page you are trying to view_PHP tutorial
When I was helping a client organize a website today, I found a prompt in Firefox: The content encoding error cannot display the page you are trying to view because it uses an invalid or unsupported compression format. I saw that the client is a PHP website, and the reason is It may be the reason for ob_gzhandler. I will summarize the solution below.
firefox error code:
Content encoding error
The page you are trying to view cannot be displayed because it uses an invalid or unsupported compression format.
Please contact the owner of the site to advise of this issue
Error cause and solution:
1. Ensure that the php program does not have any warning or error prompts
2. It is caused by PHP code ob_start('ob_gzhandler'). There are two reasons:
a. The server does not support this compression format. You can use function_exists('ob_gzhandler') to judge. The solution is to change ob_start('ob_gzhandler') to ob_start();
b. When using ob_start('ob_gzhandler'), there is already content output in the front, check the previous content and the content of the require include calling file. If it cannot be found, you can use ob_start() before calling other files, and use ob_end_clean () after calling other files to clear the output content;
3. set_magic_quotes_runtime() function:
Tip: Function set_magic_quotes_runtime() is deprecated. The reason for this prompt is that this feature has been turned off after PHP5.3. This feature has been completely removed in PHP6, which means that this function no longer exists. You can comment or delete the erroneous line, or add the @ symbol in front of set_magic_quotes_runtime().
4. PHP5.30 version no longer supports such syntax by default. Output variables need to use php echo $username;?> syntax. You can be compatible with the original syntax by setting short_open_tag in php.ini to On.
php bug about ob_start('ob_gzhandler') enabling GZIP compression
If you use ob_start("ob_gzhandler");
Then the output after ob_clean() will not be displayed. This is a bug,
You can use ob_end_clean();ob_start("ob_gzhandler"); instead of ob_clean();
Otherwise, the subsequent output content will be empty.
error_reporting(E_ALL);
ob_start("ob_gzhandler");
echo "content";
ob_clean();
echo "more content";
?>
The above code expects to output more content but actually outputs nothing.
It will be normal below
error_reporting(E_ALL);
ob_start("ob_gzhandler");
echo "content";
ob_end_clean();
ob_start("ob_gzhandler");
echo "more content";
?>
Let’s customize a callback function and test it again
function my_ob_gzhandler($buffer,$mod){
header("Content-Encoding: gzip");
Return gzencode($buffer, 9, FORCE_GZIP);
}
error_reporting(E_ALL);
ob_start("my_ob_gzhandler");
echo "content";
ob_clean();
echo "more content";
?>
The above is normal, but using ob_end_clean instead of ob_clean will cause the subsequent output to not be displayed.
So even the following code will still cause the output to be empty after using ob_clean or ob_end_clean.
if (ini_get('zlib.output_compression')) {
if (ini_get('zlib.output_compression_level') != 9) {
ini_set('zlib.output_compression_level', '9');
}
ob_start();
} else {
if (strstr($_SERVER['HTTP_ACCEPT_ENCODING'], "gzip")) {
ob_start("ob_gzhandler");
} else {
ob_start();
}
}
?>
The most stable method of enabling page compression should be similar to the following
if(extension_loaded('zlib')) {
ini_set('zlib.output_compression', 'On');
ini_set('zlib.output_compression_level', '3');
}
?>
But if you must use ob_gzhandler to enable page compression, you must pay attention to the first sentence of this article.
In fact, the following code is just not displayed by the browser
error_reporting(E_ALL);
ob_start("ob_gzhandler");
echo "content";
ob_clean();
echo "more content";
But if you test it
telnet localhost 80
GET /test.php HTTP/1.0
The following information will be returned
HTTP/1.1 200 OK
Date: Fri, 20 Feb 2009 15:40:17 GMT
Server: Apache/2.2.6 (Win32) PHP/5.2.5
X-Powered-By: PHP/5.2.5
Vary: Accept-Encoding
Content-Length: 12
Connection: close
Content-Type: text/html
more content
Lost connection to host.
You can see that more content has been output