68 行
1.7 KiB
PHP
68 行
1.7 KiB
PHP
<?php
|
|
/**
|
|
* 页面混淆和Gzip压缩工具
|
|
* 当页面包含此文件时,会自动混淆整个页面内容并进行Gzip压缩
|
|
*/
|
|
|
|
// 启用输出缓冲
|
|
ob_start();
|
|
|
|
// 注册关闭函数,在脚本结束时处理输出
|
|
register_shutdown_function('minify_output');
|
|
|
|
/**
|
|
* 处理输出:混淆HTML并进行Gzip压缩
|
|
*/
|
|
function minify_output() {
|
|
// 获取缓冲的内容
|
|
$output = ob_get_clean();
|
|
|
|
if (empty($output)) {
|
|
return;
|
|
}
|
|
|
|
// 混淆HTML内容
|
|
$output = minify_html($output);
|
|
|
|
// 检查浏览器是否支持Gzip压缩
|
|
$accept_encoding = isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : '';
|
|
$can_gzip = (extension_loaded('zlib') && strpos($accept_encoding, 'gzip') !== false);
|
|
|
|
if ($can_gzip) {
|
|
// 进行Gzip压缩
|
|
$compressed = gzencode($output, 9);
|
|
|
|
// 设置Gzip相关的HTTP头
|
|
header('Content-Encoding: gzip');
|
|
header('Content-Length: ' . strlen($compressed));
|
|
header('Vary: Accept-Encoding');
|
|
|
|
// 输出压缩后的内容
|
|
echo $compressed;
|
|
} else {
|
|
// 直接输出未压缩的内容
|
|
echo $output;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 混淆HTML内容
|
|
* @param string $html 原始HTML内容
|
|
* @return string 混淆后的HTML内容
|
|
*/
|
|
function minify_html($html) {
|
|
// 移除HTML注释
|
|
$html = preg_replace('/<!--[\s\S]*?-->/', '', $html);
|
|
|
|
// 移除多余的空白字符
|
|
$html = preg_replace('/\s+/', ' ', $html);
|
|
|
|
// 移除标签之间的空白
|
|
$html = preg_replace('/>\s+</', '><', $html);
|
|
|
|
// 移除行首和行尾的空白
|
|
$html = trim($html);
|
|
|
|
return $html;
|
|
}
|
|
?>
|