search
Homephp教程php手册PHP如何禁止图片文件的被盗链

PHP如何禁止图片文件的被盗链

Jun 13, 2016 am 10:35 AM
phpHostpictureareahowdocumentofprohibitlinkchain

1、假设充许连结图片的主机域名为:www.test.com

2、修改httpd.conf

 SetEnvIfNoCase Referer "^http://www.test.com/" 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.)?test.com /.*$ [NC]
RewriteRule .(gif|jpg)$ http://www.test.com/abc.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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.