PHP 简单破解图片防盗链的方法
在网站根目录下创建get_img.php文件,文件内容如下:
<?php $url = $_GET["url"]; //$url = str_replace("http:/","http://",$url); //$dir = pathinfo($url); //获取图片信息 //$host = $dir['dirname']; //图片dirname $host = "http://www.baidu.com"; //这里我直接写网站的地址,使用这种方法加载速度要更快,如果使用pathinfo($url)侧使用上面的方法 $refer = $host.'/'; $ch = curl_init($url); curl_setopt ($ch, CURLOPT_REFERER, $refer); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //激活可修改页面,Activation can modify the page curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); $data = curl_exec($ch); curl_close($ch); $ext = strtolower(substr(strrchr($img,'.'),1,10)); $types = array( 'gif'=>'image/gif', 'jpeg'=>'image/jpeg', 'jpg'=>'image/jpeg', 'jpe'=>'image/jpeg', 'png'=>'image/png', ); $type = $types[$ext] ? $types[$ext] : 'image/jpeg'; header("Content-type: ".$type); echo $data; ?>
有了以上的代码,就可以这样显示图片了:
<img src="http://www.zhix.net/get_img.php?url=https://b.bdstatic.com/boxlib/20180112/201801121500149246567124.jpg" />