Break through the anti-leeching measures based on HTTP_REFERER in PHP (stream_context_create)_PHP tutorial

WBOY
Release: 2016-07-21 15:31:26
Original
718 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 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 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 more "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;
}
}

Further expansion can be done Encapsulate it into a script, and then call

http://127.0.0.1/proxy.php?url=http://i.am/img to get the links that enable anti-hotlinking measures (then use it Below, use Javascript to replace all image links).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322994.htmlTechArticleSo if you consider breaking through anti-hotlinking measures, you need to consider manipulating HTTP_REFERER. The corresponding variable in the PHP script is $_SERVER['HTTP_REFERER'], which stores HTTP_REFER...
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