Loading... <div class="tip share">请注意,本文编写于 1493 天前,最后修改于 1493 天前,其中某些信息可能已经过时。</div> ## 前言 最近,新浪云对图床做了Referer防盗链验证,导致广大使用新浪当图床的博主的文章内的引用素材失效,无法显示,为此,我们可以经过中转或者伪装等方式来绕过Referer防盗链验证 ### Referer防盗链是什么 > HTTP Referer是Header的一部分,当浏览器向web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器藉此可以获得一些信息用于处理。比如从我主页上链接到一个朋友那里,他的服务器就能够从HTTP Referer中统计出每天有多少用户点击我主页上的链接访问他的网站。 基于Referer我们就可以在服务端知道哪些是“友军”哪些是外链,从而达到过滤和节约服务器资源的目的。 我们的解决方案就是通过不发送referer的方式来访问,所以就需要伪装或者中转,话不多说,贴解决方案 ## 解决方案 ### PHP文件中转 ```php <?php header('Content-type: image/jpeg'); echo file_get_contents(isset($_GET["url"])?$_GET["url"]:''); ``` 原理:通过传入Url再输出来显示图片 优缺点:**可以使用统计类组件**,效率低,需要大批量更换链接 使用方法:建立一个PHP文件在某个服务器上 http(s)://你的域名/img.php?url=照片地址 使用实例: ```php <img src="https://your-domain-name/img.php?url=https://ws3.sinaimg.cn/large/006tNbRwly1fyq4yujgssj30jg0jgmxg.jpg" /> ``` (已验证可用) ### 使用iframe ```html <script>window.sc="<img src='图片链接?"+Math.random()+"'>";</script> <iframe id="imiframe" src="javascript:parent.sc" style="border:none; overflow: hidden;" scrolling="no" frameborder="0" onload="javascript:var x=document.getElementById('imiframe').contentWindow.document.images[0];this.width=x.width+10;this.height=x.height+10;"></iframe> ``` 原理:通过iframe引用 优缺点:慢,吧 (未验证) ### Curl ```php <?php $url = $_GET["url"]; $dir = pathinfo($url); $host = $dir['dirname']; $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; ``` 原理:不发送Referer 优缺点:**可以使用统计类组件**,慢,需要大批量更换链接 使用方法:建立一个PHP文件在某个服务器上 http(s)://你的域名/img.php?url=照片地址 使用实例: ```php <img src="https://your-domain-name/img.php?url=https://ws3.sinaimg.cn/large/006tNbRwly1fyq4yujgssj30jg0jgmxg.jpg" /> ``` (已验证可用) ### Meta 标签 ```html <meta name="referrer" content="never"> ``` **原理:**不发送Referer,通过指定 name 值为 referrer 的 meta 标签 优缺点:统计类组件无法使用,因为统计也是通过这个原理 (未验证) ### CSP 响应头 ```html <meta http-equiv="Content-Security-Policy" content="referrer"> ``` **原理:**CSP(Content Security Policy),是一个跟页面内容安全有关的规范。在 HTTP 中通过响应头中的 Content-Security-Policy 字段来告诉浏览器当前页面要使用何种 CSP 策略 (未验证) ### `<a> `标签的 Referer 属性 ```html <a href="https://www.xsy.fun" referrer="no-referrer">XSY的FUN天地</a> ``` **原理:**在`<a> `标签内添加no-referrer策略 优缺点:这种方式作用的只是这一个链接。 并且,`<a> `标签可用的 Referrer 策略只有三种:no-referrer、origin 以及 unsafe-url 。 另外,这样针对单个链接设置的策略优先级比CSP要高。 ## Emmm 最好的办法就是使用自建图床或者使用对象储存 推荐阿里云的OSS,资费低,速度快,可以使用CDN全球加速(另收费) 云产品通用红包:[点我](https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=w7yzqy3r "点我") 另外,**迁移方案**已经有大佬放出 参见Github:[链接](https://github.com/ihewro/PullSinaimgtoTypecho "链接") Last modification:April 27, 2019 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 0 If you think my article is useful to you, please feel free to appreciate