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;
}
}
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.