php如何控制用户对图片的访问 PHP禁止图片盗链,
php如何控制用户对图片的访问 PHP禁止图片盗链,
把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();
这段代码是读取图片,然后直接输出给浏览器,在读取和输出之前,进行用户权限判断。
这里说的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 这张“禁止盗链”的图片!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
