Home php教程 php手册 php中突破基于HTTP_REFERER的防盗链措施(stream_context_create)

php中突破基于HTTP_REFERER的防盗链措施(stream_context_create)

Jun 06, 2016 pm 08:36 PM
Anti-hotlinking

如果考虑突破防盗链的措施,就需要考虑在 HTTP_REFERER 上面做手脚了。很多网站是通过referer来判断是否盗链。

那么如果考虑突破防盗链的措施,就需要考虑在 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 (使用 )然后获取对方的数据(使用 file_get_contents,需要开启 )。

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

另外,再提供个获取主机名的正则函数
代码如下:
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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)