PHP anti-hotlink function example code

怪我咯
Release: 2023-03-13 19:58:01
Original
1512 people have browsed it

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]");
}
?>
Copy after login

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!

Related labels:
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