首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 簡易防盜鏈驗證實作:HTTP_REFERER 解析與域名比對防護

     編輯:PHP 綜合知識

防盜鏈原理

瀏覽器請求資源時自動附加 `Referer` 標頭 (來源頁面 URL)。伺服器解析 `Referer` 域名,若非自有域名則拒絕服務 (返回 403/重導向)。

完整實作

前端頁面 (index.php)

<!DOCTYPE html>
<html><head>
    <meta charset="gb2312">
    <title>防盜鏈測試</title>
</head><body>
<form action="check.php" method="post">
    提交資訊:<input type="text" name="name">
    <input type="submit" name="submit" value="提交">
</form>
</body></html>

驗證腳本 (check.php)

<?php
// 解析 Referer
$referer = $_SERVER['HTTP_REFERER'] ?? '';
$parsed = parse_url($referer);

echo "<pre>";
print_r($parsed);
echo "Referer: $referer";
echo "</pre>";

// 核心驗證:域名比對
$allowedHost = 'localhost'; // 生產環境改為實際域名,如 'example.com'

if (empty($parsed['host']) || $parsed['host'] !== $allowedHost) {
    // 非法來源:返回錯誤並重導向
    header('HTTP/1.1 403 Forbidden');
    echo "<script>alert('來源驗證失敗,禁止存取'); location.href='index.php';</script>";
    exit;
}

// 合法來源:繼續業務邏輯
echo "驗證通過,處理表單資料...";
print_r($_POST);
?>

進階防護策略

1. 多域名白名單

<?php
$allowedHosts = ['example.com', 'www.example.com', 'api.example.com', 'localhost'];
$host = $parsed['host'] ?? '';

if (!in_array($host, $allowedHosts)) {
    // 記錄可疑存取
    error_log("[ANTI_LEECH] Blocked: $referer from IP: {$_SERVER['REMOTE_ADDR']}");
    header('HTTP/1.1 403 Forbidden');
    exit('Access Denied');
}
?>

2. 支援子域名萬用字元

<?php
function isHostAllowed($host, $patterns) {
    foreach ($patterns as $pattern) {
        // 支援 *.example.com 寫法
        $regex = '/^' . str_replace('\*', '.*', preg_quote($pattern, '/')) . '$/i';
        if (preg_match($regex, $host)) return true;
    }
    return false;
}

$allowedPatterns = ['*.example.com', 'api.partner.com'];
if (!isHostAllowed($parsed['host'] ?? '', $allowedPatterns)) {
    exit('Forbidden');
}
?>

3. 空 Referer 處理策略

策略適用場景風險
允許空 Referer直接訪問、書籤、郵件連結、部分防火牆/代理過濾繞過防盜鏈
拒絕空 Referer高安全性資源 (付費內容、API)誤傷正常用戶
Token 驗證最安全:頁面生成時植入 Token,請求攜帶 Token 驗證實作複雜度較高

Nginx/Apache 層面防盜鏈 (效能更佳)

# Nginx
location ~* \.(jpg|png|gif|css|js|pdf)$ {
    valid_referers none blocked *.example.com example.com;
    if ($invalid_referer) {
        return 403;
        # 或重導向預設圖片
        # rewrite ^/ https://example.com/images/nohotlink.png;
    }
}
# Apache .htaccess
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com [NC]
RewriteRule \.(jpg|png|gif|css|js)$ - [F,NC]

局限性與補充

  • ⚠️ `Referer` 可偽造 (curl `-H "Referer: ..."`、瀏覽器擴充套件)
  • ⚠️ HTTPS → HTTP 請求不送 Referer (安全策略)
  • ⚠️ 隱私模式/防火牆/企業代理可能移除 Referer
  • ✅ 關鍵資源建議結合 Token 驗證 或 簽名 URL (含過期時間)
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved