Anti-hotlinking is a very common web programming technique nowadays. This article uses an example to demonstrate the implementation method of PHP anti-hotlinking. Share it with everyone for your reference. The specific method is as follows:
The main function codes are as follows:
<?php /* 使用方法:将上述代码保存为dao4.php, 比如测试用的validatecode.rar在站点http://www.XXXX.com里面, 则用以下代码表示下载连接. 文件名?site=1&file=文件 例如以下URL: http://www.XXXX.cn/dao4.php?site=1&file=validatecode.rar */ $ADMIN[defaulturl] = "http://www.XXXX.com/404.htm"; //盗链返回的地址 $okaysites = array( "http://www.XXXX.com/", "http://XXXX.com" ); //白名单 $ADMIN[url_1] = "http://www.XXXX.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]"); } ?>
I hope this article will be helpful to everyone learning PHP programming.
Implementing this with PHP consumes more resources. It is recommended to start with the server and change the apache configuration
The following is the code for PHP implementation. xxx.mp3 is the actual address of your music file. When distributing to the outside world, only the PHP address and mp3 address are transmitted. Not public to the public
if(strpos($_SERVER['HTTP_REFERER'], 'qq.com') !== FALSE) {
header('HTTP/1.1 404 Not Found' );
exit;
}
readfile('xxx.mp3');
?>
It can be realized. You can search on the Internet for the method of outputting pictures through php binary stream. Since the picture output needs to go through php, you can use php to judge it first