Breakthrough in anti-leeching measures stream_context_create based on HTTP_REFERER in PHP

WBOY
Release: 2016-07-29 08:44:40
Original
853 people have browsed it

So if you consider taking measures to prevent hotlinking, you need to consider manipulating HTTP_REFERER. The corresponding variable in the PHP script is $_SERVER['HTTP_REFERER'], which stores the value of HTTP_REFERER.
Since direct access to the target URL resource has been blocked by the above anti-hotlinking measures, we need something similar to a gateway to obtain it. To put it bluntly, it is to write a PHP script that has wrapped HTTP headers.
The following is a simple function implementation:

Copy the code The code is as follows:


function getRemoteFile($url, $refer = '') {
$option = array(
'http' => array(
'header' => "Referer:$refer")
);
$context = stream_context_create($option);
return file_get_contents($url, false, $context);
}


This is a relatively simple one function, its function is to forge the Referer (using the stream_context_create function) and then obtain the other party's data (using file_get_contents, you need to enable allow_url_fopen).

If you want to be "complex", you can use sockets extension, which is beyond the scope of discussion here.

In addition, provide a regular function to get the host name

Copy the code The code is as follows:


function getHost($url) {
$result = preg_match('/^http://([d |w|.]+)//', $url, $matches);
if (sizeof($matches) >= 2) {
return $matches[1];
} else {
return null;
}
}


can be further expanded and can be encapsulated into a script, and then call
http://127.0.0.1/proxy.php?url=http://i.am/img to obtain those anti-hotlinking measures. links (to further develop, use Javascript to replace all image links).

The above introduces stream_context_create, the anti-hotlinking measure based on HTTP_REFERER in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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