Home > Article > Backend Development > How to prohibit external hotlinking in php
php method to prohibit external hotlinking: first find and open the ".htaccess" file; then add the content "RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ [NC]" to the file; finally save it This file will do.
Recommended: "PHP Video Tutorial"
PHP Methods to Prevent Hotlinking
Anti-hotlinking technology is already very common. Some websites don’t like their pictures to be directly copied and used by other websites, so they use anti-hotlinking technology. In this way, when others directly copy and use website pictures, the pictures will be According to the program settings, words such as anti-hotlinking are not displayed or displayed.
Using anti-hotlinking technology, it can not only prevent your pictures from being stolen, but also save the traffic of downloading pictures on your own site. I think it is quite good, so how to prevent hotlinking in the PHP environment? In the PHP environment, we know that the Apache server is usually used, so let’s mainly look at the Apache anti-hotlink method. In fact, the same principle is used in IIS.
Apache anti-hotlinking:
Most virtual hosts are Apache, so the most convenient anti-hotlinking setting is to use the .htaccess file. There are many ways to search on the Internet, so I summarized the methods that are absolutely easy to use. Add the following code to the .htaccess file and modify it.
RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ [NC] RewriteCond %{HTTP_REFERER} !60ie.net [NC] RewriteCond %{HTTP_REFERER} !youdao.com [NC] RewriteCond %{HTTP_REFERER} !zhuaxia.com [NC] RewriteCond %{HTTP_REFERER} !twitter.com [NC] RewriteCond %{HTTP_REFERER} !facebook.com [NC] RewriteCond %{HTTP_REFERER} !xianguo.com [NC] RewriteCond %{HTTP_REFERER} !google.cn [NC] RewriteCond %{HTTP_REFERER} !google.com [NC] RewriteCond %{HTTP_REFERER} !google.com.tw [NC] RewriteCond %{HTTP_REFERER} !google.com.sg [NC] RewriteCond %{HTTP_REFERER} !google.com.hk [NC] RewriteCond %{HTTP_REFERER} !bloglines.com [NC] RewriteCond %{HTTP_REFERER} !soso.com [NC] RewriteCond %{HTTP_REFERER} !mail.qq.com [NC] RewriteCond %{HTTP_REFERER} !cn.bing.com [NC] RewriteCond %{HTTP_REFERER} !image.baidu.com [NC] RewriteCond %{HTTP_REFERER} !feedburner.com [NC] RewriteCond %{HTTP_REFERER} !feedsky.com [NC] RewriteRule .(png|jpg|gif)$ [R,NC,L]
Code explanation:
Look at the last line first. I have protected the png, jpg, and gif files on the website against hotlinking. Once the pictures on my website are outside the above whitelist appears on the website, all images in the last line of code are displayed.
Nginx anti-hotlinking:
Nginx does not support .htaccess, and it is a little troublesome to set up. First, open the file /usr/local/nginx/conf/nginx.conf with a text editor (if you are using vhost, go to vhost to find the corresponding conf file
), and match the following code Just inside the server{} section. Note that I am not talking about copying, but matching.
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; valid_referers none blocked *.ilucong.com *.youdao.com *.zhuaxia.com *.xianguo.com *.google.cn *.google.com *.google.com.tw *.google.com.sg *.google.com.hk *.bloglines.com image.soso.com cn.bing.com image.baidu.com *.feedburner.com *.feedsky.com; if ($invalid_referer) { rewrite ^/ ; #return 404; } }
Note: Try not to use the Notepad that comes with Windows to edit, as erratic lines may appear; after editing, restart the Ngnix service to take effect
The above is the detailed content of How to prohibit external hotlinking in php. For more information, please follow other related articles on the PHP Chinese website!