Home >Backend Development >PHP Tutorial >Usage examples of $_SERVER['HTTP_REFERER'] to prevent hotlinking in PHP

Usage examples of $_SERVER['HTTP_REFERER'] to prevent hotlinking in PHP

PHP中文网
PHP中文网Original
2017-03-30 12:25:351744browse
Introducing a PHP code to prevent hotlinking, which mainly uses $_SERVER["HTTP_REFERER"]. Friends in need can refer to it.

The code is as follows:

http://bbs.it-home.org
*/
session_start();  
if(!isset($_SESSION['id'])or !isset($_SESSION['member'])){  
    echo "";//验证session  
exit();  
}  
$ref=$_SERVER['HTTP_REFERER'];  
if($ref==''){  
echo '对不起,不允许从地址栏访问';  
}else{  
$url=parse_url($ref);  
if($url[host]!='127.0.0.1'&& $url[host]!='localhost'){  
  echo '不允许盗链';  
  exit();  
}  
}  
?>

Explanation of "HTTP_REFERER": The URL address of the previous page that links to the current page. Not all user agents (browsers) will set this variable, and some can also modify HTTP_REFERER manually. Therefore, this variable is not always true.

Summary: Only click on the hyperlink (i.e.) Only the opened page has the HTTP_REFERER environment variable. Other windows opened such as window.open(), window.location=..., window.showModelessDialog(), etc. do not have the HTTP_REFERER environment variable.

The above is the usage example of PHP to prevent hotlinking $_SERVER["HTTP_REFERER"]. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
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