<?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>程默的博客 &#187; header</title>
	<atom:link href="http://blog.chacuo.net/tag/header/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.chacuo.net</link>
	<description>web原理、web架构、web安全、web性能、服务器性能、服务器架构、服务器安全;你不能预知明天，但你可以利用今天。你不能样样顺利，但你可以事事尽力!</description>
	<lastBuildDate>Mon, 31 Aug 2020 15:33:40 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>php header 重定向常出现功能(使用)漏洞</title>
		<link>http://blog.chacuo.net/276.html</link>
		<comments>http://blog.chacuo.net/276.html#comments</comments>
		<pubDate>Sat, 15 Jun 2013 09:45:28 +0000</pubDate>
		<dc:creator>程默</dc:creator>
				<category><![CDATA[web安全]]></category>
		<category><![CDATA[header]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.chacuo.net/?p=276</guid>
		<description><![CDATA[php header函数功能强大，可以给用户发送各种header头信息，只要header头里面能够发送的内容， [...]]]></description>
				<content:encoded><![CDATA[<p>php header函数功能强大，可以给用户发送各种header头信息，只要header头里面能够发送的内容，都可以通过header函数来完成。如发送302跳转，设置cookie，发送401认证，发现last-modify等等！ HTTP/1.1 specification for more information on <acronym>HTTP</acronym> headers，都可以用该函数完成。总之一句话，任何服务区response应答的头信息几乎都可以用它来发送。如：</p>
<blockquote>
<pre><a href="http://blog.chacuo.net/wp-content/uploads/2013/06/image34.png"><img style="background-image: none; margin: 0px 0px 0px 10px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;" title="response 服务器响应头" alt="response 服务器响应头" src="http://blog.chacuo.net/wp-content/uploads/2013/06/image_thumb34.png" width="572" height="222" border="0" /></a></pre>
</blockquote>
<pre></pre>
<pre>这里我们经常用header发送302跳转，会发现一个问题。在走查代码时候，遇到很多同人会有类似操作代码，如：</pre>
<blockquote>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</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;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: text/html; charset=utf-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//判断用户是否授权</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'用户未授权'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;location:forbidden.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">///下面是进行摸个记录操作</span>
<span style="color: #666666; font-style: italic;">//删除条记录，或者修改某条记录……</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&nbsp;</pre></td></tr></table></div>

</blockquote>
<p>这类代码，在开发中很常见，我们会发现，一个没有权限用户，操作该记录，网页会跳转到未授权页面，但是：<strong>记录任然被修改了</strong>，问题我们找到了，一般操作人员，对于js跳转会知道是前端浏览器跳转的。但是对于php操作跳转，<span style="color: #9b00d3;"><span style="text-decoration: underline;">错误认为是服务器跳转，好像php以执行到location，然后就终止了下面运行。 </span></span><span style="color: #000000;">其实，知道header函数真实意义就很容易理解了：<b>header()</b> is used to send a raw <acronym>HTTP </acronym>header！</span></p>
<p>header location 302实际上只是如下：</p>
<blockquote>
<pre><a href="http://blog.chacuo.net/wp-content/uploads/2013/06/image35.png"><img style="background-image: none; margin: 0px 0px 0px 10px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;" title="response 服务器302响应头" alt="response 服务器302响应头" src="http://blog.chacuo.net/wp-content/uploads/2013/06/image_thumb35.png" width="576" height="266" border="0" /></a></pre>
<pre>跟其它普通echo 函数没有多大区别，只是给header信息头,echo 一行而已。</pre>
<pre></pre>
</blockquote>
<p><strong>屏蔽Bug方法：</strong></p>
<pre></pre>
<p>知道了，发送header跳转，服务器不会终止程序，因此为了安全起见。<span style="text-decoration: underline;"><span style="color: #9b00d3;">header locaction后，最好加一句exit()函数。</span></span> 这样，php解析引擎就会停止解析了！我们正确方法是封装个header_302方法</p>
<blockquote>

<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
</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: #666666; font-style: italic;">/*header_302跳转*
 * *
 *@author http://blog.chacuo.net/
 * @param string $url 跳转url
 */</span>
<span style="color: #000000; font-weight: bold;">function</span>  header_302<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">headers_sent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'header 已经发送过！'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;location:<span style="color: #006699; font-weight: bold;">$url</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</blockquote>
<p>后记：这类问题，不光出在php程序中，其它很多header loction跳转语言程序，原理相同。都有类似功能，很多时候。都是使用时候没有注意，造成的。 容易在代码走查中，遗漏掉！好了，先到这里，欢迎交流！</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chacuo.net/276.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
