1、假設(shè)充許連結(jié)圖片的主機(jī)域名為:www.test.com
2、修改httpd.conf
setenvifnocase referer "^http://www.test.com/" local_ref=1
<filesmatch ".(gif|jpg)">
order allow,deny
allow from env=local_ref
</filesmatch>
這個(gè)簡(jiǎn)單的應(yīng)用不光可以解決圖片盜鏈的問題,稍加修改還可以防止任意文件盜鏈下載的問題。
使用以上的方法當(dāng)從非指定的主機(jī)連結(jié)圖片時(shí),圖片將無法顯示,如果希望顯示一張“禁止盜鏈”的圖片,我們可以用mod_rewrite 來實(shí)現(xiàn)。
首先在安裝 apache 時(shí)要加上 --enable-rewrite 參數(shù)加載 mod_rewrite 模組。
假設(shè)“禁止盜鏈”的圖片為abc.gif,我們?cè)?httpd.conf 中可以這樣配置:
rewriteengine on
rewritecond %{http_referer} !^$
rewritecond %{http_referer} !^http://(www/.)?test.com /.*$ [nc]
rewriterule /.(gif|jpg)$ http://www.test.com/abc.gif [r,l]
當(dāng)主機(jī)的圖片被盜鏈時(shí),只會(huì)看到 abc.gif 這張“禁止盜鏈”的圖片!
一段防盜連的php代碼
<?php
$admin[defaulturl] = "http://www.163.com/404.htm";//盜鏈返回的地址
$okaysites = array("http://www.163.com/","http://163.com"); //白名單
$admin[url_1] = "http://www.163.com/download/";//下載地點(diǎn)1
$admin[url_2] = "";//下載地點(diǎn)2,以此類推
$reffer = $http_referer;
if($reffer) {
$yes = 0;
while(list($domain, $subarray) = each($okaysites)) {
if (ereg($subarray,"$reffer")) {
$yes = 1;
}
}
$theu = "url"."_"."$site";
if ($admin[$theu] and $yes == 1) {
header("location: $admin[$theu]/$file");
} else {
header("location: $admin[defaulturl]");
}
} else {
header("location: $admin[defaulturl]");
}
?>
使用方法:將上述代碼保存為dao4.php
最大的網(wǎng)站源碼資源下載站,
新聞熱點(diǎn)
疑難解答