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

PHP中文网
Release: 2023-02-28 20:34:02
Original
1661 people have browsed it
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();  
}  
}  
?>
Copy after login

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 (m.sbmmt.com)!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!