Home  >  Article  >  php教程  >  php中突破基于HTTP

php中突破基于HTTP

WBOY
WBOYOriginal
2016-06-21 08:54:50706browse

那么如果考虑突破防盗链的措施,就需要考虑在 HTTP_REFERER 上面做手脚了。PHP 脚本中对应的变量是 $_SERVER['HTTP_REFERER'] ,它存储了 HTTP_REFERER 的值。

由于直接访问目标 URL 资源已经被上述防盗链的措施给屏蔽,所以我们需要个类似网关的玩意去获取。说白了就是编写已经包装过的 HTTP 头的 PHP 脚本。

下面是简单的函数实现:

复制代码 代码如下:


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


这是个比较简单的函数,其功能就是伪造 Referer (使用 stream_context_create 函数)然后获取对方的数据(使用 file_get_contents,需要开启 allow_url_fopen )。

如果想“复杂”一点,可以使用 sockets 扩展,这不在这里的讨论范围以内。

另外,再提供个获取主机名的正则函数

复制代码 代码如下:


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


再进一步的扩展,可以封装成脚本,然后譬如调用

http://127.0.0.1/proxy.php?url=http://i.am/img就可以获取那些开启防盗链措施的链接了(再发挥下,使用 Javascript 将图片链接全部替换)。



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