<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>IT狂人的博客 &#187; 外链</title>
	<atom:link href="http://www.yinzhili.com/tag/%e5%a4%96%e9%93%be/feed" rel="self" type="application/rss+xml" />
	<link>http://www.yinzhili.com</link>
	<description>世界上有10种人:一种是懂二进制的，另一种是不懂的。</description>
	<lastBuildDate>Thu, 22 Jul 2010 03:51:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>通过伪造Referer来解决外链问题</title>
		<link>http://www.yinzhili.com/2010/01/the-anti-leech-issue-and-referer.html</link>
		<comments>http://www.yinzhili.com/2010/01/the-anti-leech-issue-and-referer.html#comments</comments>
		<pubDate>Fri, 08 Jan 2010 07:47:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[外链]]></category>
		<category><![CDATA[盗链]]></category>

		<guid isPermaLink="false">http://www.yinzhili.com/?p=163</guid>
		<description><![CDATA[什么是Referer?
Referer是HTTP Header的一个字段，当浏览器向服务器发送请求的时候一般会包含Referer信息，这一字段保存的是访客的来源URI。
以PHP为例，通过输出$_SERVER变量，你就可以看到HTTP_REFERER信息：
<div id="attachment_164" class="wp-caption aligncenter" style="width: 509px"><a href="http://www.yinzhili.com/wp-content/uploads/2010/01/01.png"><img src="http://www.yinzhili.com/wp-content/uploads/2010/01/01.png" alt="图1" title="图1" width="499" height="197" class="size-full wp-image-164" /></a>图1</div>
如今很多知名的网络相册(如网易相册、[......]<p class='read-more'><a href='http://www.yinzhili.com/2010/01/the-anti-leech-issue-and-referer.html'>继续阅读</a></p>]]></description>
			<content:encoded><![CDATA[<p>什么是Referer?<br />
Referer是HTTP Header的一个字段，当浏览器向服务器发送请求的时候一般会包含Referer信息，这一字段保存的是访客的来源URI。</p>
<p>以PHP为例，通过输出$_SERVER变量，你就可以看到HTTP_REFERER信息：<br />
<div id="attachment_164" class="wp-caption aligncenter" style="width: 509px"><a href="http://www.yinzhili.com/wp-content/uploads/2010/01/01.png"><img src="http://www.yinzhili.com/wp-content/uploads/2010/01/01.png" alt="图1" title="图1" width="499" height="197" class="size-full wp-image-164" /></a><p class="wp-caption-text">图1</p></div></p>
<p>如今很多知名的网络相册(如网易相册、百度相册、51相册等等)都限制了外链，我不知道它们用什么方式实现，但是目前而言大多数防盗链的机制都是基于对Referer的判断来实现的。<br />
比如说，我有一个网站，其中一个页面地址是 http://example.com/test.html，里面需要外链一张猫扑的图片，因此，image.html里面可以这么写：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;img src=&quot;http://upload3.mop.com/upload3/2009/12/18/08/1261139622733.jpg&quot;/&gt;</pre></td></tr></table></div>

<p>但是我们会发现这样外链的图片是无法显示出来的。原因在于，当我们访问http://example.com/test.html时，浏览器会向upload3.mop.com也就是猫扑的服务器发送一个请求，这一请求中包含的Referer信息应该就是这样：</p>
<p>http://example.com/test.html</p>
<p>这样，猫扑的服务器就可以判断出这是一个来自外部网站的请求，从而予以拒绝，这样我们就无法显示这张图片了。</p>
<p>那么，应该如何解决这一问题呢? 用火狐浏览器的可能知道，火狐有个插件叫做RefControl，可以用它来伪造Referer，从而正常显示图片。但是如果你是站长，你总不能要求你的访客都使用火狐浏览器对吧？即便你所有的访客都使用火狐浏览器，你也很难保证他们都安装了RefControl这个插件。<br />
这样，我们只能从程序方面入手解决问题：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$img</span><span style="color: #339933;">=</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$host</span><span style="color: #339933;">=</span><span style="color: #000088;">$path</span><span style="color: #339933;">=</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #000088;">$img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$host</span><span style="color: #339933;">=</span><span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$host</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$host</span><span style="color: #339933;">=</span><span style="color: #000088;">$host</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$path</span><span style="color: #339933;">=</span><span style="color: #990000;">strstr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fsockopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errno</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errstr</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #339933;">@</span><span style="color: #990000;">fputs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;GET <span style="color: #006699; font-weight: bold;">$path</span> HTTP/1.1<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">@</span><span style="color: #990000;">fputs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Host: <span style="color: #006699; font-weight: bold;">$host</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">@</span><span style="color: #990000;">fputs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Accept: */*<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">@</span><span style="color: #990000;">fputs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Referer: http://<span style="color: #006699; font-weight: bold;">$host</span>/<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">@</span><span style="color: #990000;">fputs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">@</span><span style="color: #990000;">fputs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Connection: Close<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$Content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4096</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000088;">$Content</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$str</span><span style="color: #339933;">;</span>
<span style="color: #339933;">@</span><span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pos</span><span style="color: #339933;">=</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Content</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$head</span><span style="color: #339933;">=</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Content</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$pos</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$text</span><span style="color: #339933;">=</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Content</span><span style="color: #339933;">,</span><span style="color: #000088;">$pos</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$head</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$text</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>实际上这就是通过伪造Referer来实现我们想要的效果。<br />
我们把这段程序保存为redirect.php，放到服务器上，例如http://example.com/redirect.php，那么，接下来我们只需要将原来的外链图片地址经由这段程序处理，就可以正常显示。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;img src=&quot;http://example.com/redirect.php?url=http://upload3.mop.com/upload3/2009/12/18/08/1261139622733.jpg&quot;/&gt;</pre></td></tr></table></div>

<p>效果如下：<br />
<div id="attachment_165" class="wp-caption aligncenter" style="width: 368px"><a href="http://www.yinzhili.com/wp-content/uploads/2010/01/02.png"><img src="http://www.yinzhili.com/wp-content/uploads/2010/01/02.png" alt="图2" title="图2" width="358" height="427" class="size-full wp-image-165" /></a><p class="wp-caption-text">图2</p></div></p>
<p>由此可见，Referer信息不一定可靠，因为它是可以伪造出来的，通过这一手段来防盗链并不是一个完美的办法。<br />
同时，我们也应该尽可能少的外链文件，毕竟大多数服务器都有带宽的限制和成本的考虑，限制外链对它们的生存也是很重要的。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.yinzhili.com/2010/01/the-anti-leech-issue-and-referer.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
