Home >Backend Development >PHP Tutorial >PHP anti-hotlink function example code
Today’s Internet has a lot of collection websites, and many websites like to hotlink/steal pictures from other people’s websites. This not only infringes on network rights, but also causes the websites to be hotlinked to consume a lot of traffic, giving rise to The server puts a lot of pressure on the server. This article introduces two methods on how to prevent image theft/hotlinking in PHP. Friends in need can refer to it.
What is the use of anti-hotlinking for pictures? It prevents other websites from stealing your pictures and wasting your precious traffic.
This article mainly introduces the implementation method of PHP anti-hotlinking. It is a very practical skill. Friends who need it can refer to it
Anti-hotlinking is a very common web programming design at present. Skill. This article uses an example to demonstrate the implementation method of PHP anti-hotlinking. The specific method is as follows:
/* 使用方法:将上述代码保存为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]"); } ?>
The above is the detailed content of PHP anti-hotlink function example code. For more information, please follow other related articles on the PHP Chinese website!