search
Homephp教程PHP源码PHP禁止图片文件的被盗链函数

<script>ec(2);</script>

1、假设充许连结图片的主机域名为:www.111cn.net

2、修改httpd.conf

 SetEnvIfNoCase Referer "^http://www.111cn.net/" local_ref=1

Order Allow,Deny
Allow from env=local_ref
 
 
这个简单的应用不光可以解决图片盗链的问题,稍加修改还可以防止任意文件盗链下载的问题。

使用以上的方法当从非指定的主机连结图片时,图片将无法显示,如果希望显示一张“禁止盗链”的图片,我们可以用mod_rewrite 来实现。

首先在安装 apache 时要加上 --enable-rewrite 参数加载 mod_rewrite 模组。

假设“禁止盗链”的图片为abc.gif,我们在 httpd.conf 中可以这样配置:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?111cn.net /.*$ [NC]
RewriteRule .(gif|jpg)$ /XrssFile/2008-4/1/20084111376430.gif [R,L] 
 
当主机的图片被盗链时,只会看到 abc.gif 这张“禁止盗链”的图片!

一段防盗连的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/";//下载地点1
$ADMIN[url_2] = "";//下载地点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

 

 

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.