Home >Backend Development >PHP Tutorial >PHP prevents hotlinking by judging the source host header

PHP prevents hotlinking by judging the source host header

WBOY
WBOYOriginal
2016-07-29 09:16:201065browse

check.php

<span><</span><span>html</span><span>></span><span><</span><span>body</span><span>></span><span><</span><span>form </span><span>action</span><span>="test.php"</span><span> method</span><span>="post"</span><span>></span><span>    message</span><span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="name"</span><span> value</span><span>="123456"</span><span>/></span><span><</span><span>input </span><span>type</span><span>="submit"</span><span> value</span><span>="submit"</span><span>/></span><span></</span><span>form</span><span>></span><span></</span><span>body</span><span>></span><span></</span><span>html</span><span>></span>

test.php

<?<span>php
</span><span>header</span>("Content-type:text/html;charset='utf8'"<span>);
</span><span>error_reporting</span>(<span>E_ALL</span><span>);

</span><span>$url</span> = <span>parse_url</span>(<span>$_SERVER</span>['HTTP_REFERER'<span>]);
</span><span>//</span><span>print_r($url);</span><span>if</span>(<span>$url</span>['host'] != "localhost"<span>){
    </span><span>echo</span> "<script>alert('host error.');history.go(-1);</script>"<span>;
    </span><span>exit</span>(0<span>);
}</span><span>else</span><span>{
    </span><span>echo</span> "<script>alert('host right.');</script>"<span>;
    </span><span>echo</span> "<h1>Hello World!</h1>"<span>;
}</span>

The above introduces how PHP prevents hotlinking by judging the source host header, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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