php如何控制用户对图片的访问 PHP禁止图片盗链_php技巧

WBOY
Release: 2016-05-16 19:55:46
Original
1123 people have browsed it

把images目录设置成不充许http访问(把图片目录的:读取、目录浏览 两个权限去掉)。
用一个PHP文件,直接用file函数读取这个图片。在这个PHP文件里进行权限控制。
apache环境中,在你的图片目录中加上下面这个文件即可。

文件名 .htaccess
文件内容如下

复制代码 代码如下:

# options the .htaccess files in directories can override.
# Edit apache/conf/httpd.conf to AllowOverride in .htaccess
# AllowOverride AuthConfig
# Stop the directory list from being shown
Options -Indexes
# Controls who can get stuff from this server.
Order Deny,Allow
Deny from all
Allow from localhost

其他web环境如iss,nginx也类似。

class imgdata{
public $imgsrc;
public $imgdata;
public $imgform;
public function getdir($source){
$this->imgsrc = $source;
}
public function img2data(){
$this->_imgfrom($this->imgsrc);
return $this->imgdata=fread(fopen($this->imgsrc,'rb'),filesize($this->imgsrc));
}
public function data2img(){
header(“content-type:$this->imgform”);
echo $this->imgdata;
//echo $this->imgform;
//imagecreatefromstring($this->imgdata);
}
public function _imgfrom($imgsrc){
$info=getimagesize($imgsrc);
//var_dump($info);
return $this->imgform = $info['mime'];
}
}
$n = new imgdata;
$n -> getdir(“1.jpg”); //图片路径,一般存储在数据库里,用户无法获取真实路径,可根据图片ID来获取
$n -> img2data();
$n -> data2img();
Copy after login

这段代码是读取图片,然后直接输出给浏览器,在读取和输出之前,进行用户权限判断。
这里说的PHP读取图片,不是指读取路径,而是指读取图片的内容,然后通过
Header();输入图片类型,比如 gif png jpg等,下面输出图片的内容,所以用到了fread()
实际上,你看到 image.php?id=100 就是显示这张图片在浏览器上,而你查看源文件,看到的不会是图片的路径,而是乱码似的图片内容。
===========================================
类似于qq空间的加密相册,只有输入密码才能访问,并且直接在浏览器输入 加密相册中的相片地址也是无法访问。我目前的想法是 图片的地址是一个php文件,通过 php 验证权限 ,读取图片,并输出,不知道除了这样的方法还有更简单高效的做法没有?比如生成临时的浏览地址,使用一些 nginx 的一些防盗链插件?
你可以利用ngx_http_auth_basic_module来完成。

修改配置文件

复制代码 代码如下:

location / {
root /usr/local/nginx/html;
auth_basic “Auth”;
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
index index.php index.htm;
}


auth_basic “Auth”中的Auth是弹出框(输入用户名和密码)的标题
auth_basic_user_file /usr/local/nginx/conf/htpasswd; 中的/usr/local/nginx/conf/htpasswd是保存密码的文件


PHP禁止图片盗链
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 这张“禁止盗链”的图片!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!