php图片防盗链的小测试
test.php <?php $txt = "http://hiphotos.baidu.com/stupidet/pic/item/4f1b8cfb4c33b7254e4aea69.jpg"; //$txt=$_GET['url']; header("content-type:image/jpeg"); echo referfile($txt,''); exit('xxxx'); function referfile($url,$refer='http://image.baidu.com/') { //这里把$refer=''设为空,也可以。 $opt=array('http'=>array('header'=>"Referer:$refer\r\nHost: hiphotos.baidu.com\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0\r\n")); $context=stream_context_create($opt); return file_get_contents($url,false,$context); } ?> test1.php: //通过了头部处理可以访问图片 <img src="test.php" />
test2.php : //直接访问无法
<img src="http://hiphotos.baidu.com/stupidet/pic/item/4f1b8cfb4c33b7254e4aea69.jpg" />