
本文讲解为何不应使用 get_headers() 检查当前站点内文件存在性,以及如何通过 file_exists() 安全、高效地实现本地资源路由分发,避免递归请求、超时和 404 警告。
本文讲解为何不应使用 `get_headers()` 检查当前站点内文件存在性,以及如何通过 `file_exists()` 安全、高效地实现本地资源路由分发,避免递归请求、超时和 404 警告。
你在 index.php 中尝试用 get_headers($url) 判断一个形如 http://example.com/assets/style.css 的 URL 是否存在,本质上是在发起一次新的 HTTP 请求回环到自身服务器——这不仅造成性能浪费,更会因未正确处理重定向或超时而触发 failed to open stream: HTTP request failed 警告,甚至引发无限递归(例如当 index.php 自身被 get_headers() 请求时再次执行路由逻辑)。
✅ 正确做法是:将 URL 路径映射为服务器本地文件系统路径,再用 file_exists() 判断。这是零网络开销、毫秒级响应、完全可控的安全方案。
✅ 推荐实现方式(无 .htaccess 依赖)
假设你的项目结构如下:
/project-root/ ├── index.php ← 入口路由 ├── assets/ │ ├── style.css │ └── logo.png ├── views/ │ └── dashboard.php └── public/ ← 可选:实际 Web 根目录(若配置了 DocumentRoot)
在 index.php 中,你需要:
- 解析请求 URI(去除查询参数,标准化路径)
- 映射到物理路径(基于 __DIR__ 或预设根目录)
- 安全校验路径合法性(防止目录遍历攻击)
- 检查文件存在性 & 类型白名单
- 直接输出或转发
<?php
// index.php
// 1. 定义允许被直接访问的静态资源目录(相对于 index.php 所在目录)
$public_dirs = [
'assets' => __DIR__ . '/assets',
'images' => __DIR__ . '/images',
'js' => __DIR__ . '/js',
];
// 2. 获取干净的请求路径(移除 query string 和 fragment)
$request_uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$request_uri = rtrim($request_uri, '/');
// 3. 防止路径遍历:拒绝含 '..' 或以 '/' 开头的非法路径
if (strpos($request_uri, '..') !== false || $request_uri[0] === '/') {
http_response_code(403);
die('Forbidden');
}
// 4. 尝试匹配静态资源路径(如 /assets/style.css)
foreach ($public_dirs as $uri_prefix => $real_path) {
if (str_starts_with($request_uri, "/{$uri_prefix}/")) {
$file_path = $real_path . substr($request_uri, strlen("/{$uri_prefix}"));
// 安全校验:确保解析后路径仍在 $real_path 内
if (realpath($file_path) === false ||
strpos(realpath($file_path), realpath($real_path)) !== 0) {
http_response_code(403);
die('Access denied');
}
if (file_exists($file_path)) {
// 设置合适的 Content-Type(简易版,生产环境建议用 mime_content_type 或扩展映射)
$ext = pathinfo($file_path, PATHINFO_EXTENSION);
$mime_types = [
'css' => 'text/css',
'js' => 'application/javascript',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'svg' => 'image/svg+xml',
];
$mime = $mime_types[$ext] ?? 'application/octet-stream';
header("Content-Type: {$mime}");
readfile($file_path);
exit;
}
}
}
// 5. 若未命中静态资源,则进入动态路由逻辑(如 MVC 分发)
// include __DIR__ . '/router.php';
// 或渲染视图:
// if (file_exists(__DIR__ . '/views' . $request_uri . '.php')) {
// include __DIR__ . '/views' . $request_uri . '.php';
// } else {
// http_response_code(404);
// include __DIR__ . '/404.php';
// }⚠️ 关键注意事项
- 永远不要对同域 URL 使用 get_headers() 做存在性校验:它本质是 cURL 请求,会绕过 PHP 进程、触发完整 HTTP 生命周期,极易导致超时、递归、日志污染。
- file_exists() 只检查本地文件系统:速度快、可靠、无副作用,但前提是路径必须准确映射。
- 务必做路径规范化与白名单校验:否则攻击者可通过 ?file=../../../etc/passwd 等构造恶意路径。
- 区分“静态资源”与“动态路由”:CSS/JS/图片等应由 index.php 直接读取并输出;PHP 视图文件则应 include(而非 header("Location: ...") 重定向),避免额外 HTTP 跳转损耗。
- 不依赖 .htaccess 是完全可行的:现代 PHP 内置服务器或 Nginx/Apache 均支持将所有请求兜底到 index.php,后续路径解析完全由 PHP 控制。
✅ 总结
用 file_exists(__DIR__ . $mapped_path) 替代 get_headers($full_url),是构建健壮、高性能原生 PHP 路由的核心实践之一。它消除了网络延迟、规避了递归风险、提升了安全性,并为后续实现缓存、压缩、版本化资源等高级功能打下坚实基础。