<?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>Life and Living &#187; header</title>
	<atom:link href="http://www.lifenlivin.com/tag/header/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lifenlivin.com</link>
	<description>Destroyed in the horizon.</description>
	<lastBuildDate>Fri, 03 Sep 2010 14:11:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Buggy header in php</title>
		<link>http://www.lifenlivin.com/2010/02/buggy-header-in-php/</link>
		<comments>http://www.lifenlivin.com/2010/02/buggy-header-in-php/#comments</comments>
		<pubDate>Sat, 13 Feb 2010 16:28:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[University life]]></category>
		<category><![CDATA[header]]></category>
		<category><![CDATA[php header function]]></category>

		<guid isPermaLink="false">http://www.lifenlivin.com/?p=131</guid>
		<description><![CDATA[Today, I got a problem in a php script. I was using header() function in the if-else branching. The code is something like
if(condition1)
{
header(&#8221;Location:dummy.php?err=1&#8243;);
}
else
{
header(&#8221;Location:dummy.php?err=2&#8243;);
}
if(condition2)
{
header(&#8221;Location:dummy.php?err=3&#8243;);
}
else
{
header(&#8221;Location:dummy.php?err=4&#8243;);
}
a contition may fullfill both condition 1 and 2. But in this case I want to go through the first if clause. I run the script and saw that the final page is [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I got a problem in a php script. I was using header() function in the if-else branching. The code is something like</p>
<p>if(condition1)</p>
<p>{</p>
<p>header(&#8221;Location:dummy.php?err=1&#8243;);</p>
<p>}</p>
<p>else</p>
<p>{</p>
<p>header(&#8221;Location:dummy.php?err=2&#8243;);</p>
<p>}</p>
<p>if(condition2)</p>
<p>{</p>
<p>header(&#8221;Location:dummy.php?err=3&#8243;);</p>
<p>}</p>
<p>else</p>
<p>{</p>
<p>header(&#8221;Location:dummy.php?err=4&#8243;);</p>
<p>}</p>
<p>a contition may fullfill both condition 1 and 2. But in this case I want to go through the first if clause. I run the script and saw that the final page is dummy.php?err=3. But I supposed to get err=1. The question is why this happened? I found its answer. The first condition is fullfilled and the page is directed to dummy.php?err=1, the script does not exit, rather it run from where it was redirected. Then the second condtion is fullfilled. So the page is redirected again to dummy.php?err=3.</p>
<p>The solution is to use exit() function after each header() function so that the script cannot run further after redirection.</p>
<p>The correct code will be,</p>
<p>if(condition1)</p>
<p>{</p>
<p>header(&#8221;Location:dummy.php?err=1&#8243;);</p>
<p>exit();</p>
<p>}</p>
<p>else</p>
<p>{</p>
<p>header(&#8221;Location:dummy.php?err=2&#8243;);</p>
<p>exit();</p>
<p>}</p>
<p>if(condition2)</p>
<p>{</p>
<p>header(&#8221;Location:dummy.php?err=3&#8243;);</p>
<p>exit();</p>
<p>}</p>
<p>else</p>
<p>{</p>
<p>header(&#8221;Location:dummy.php?err=4&#8243;);</p>
<p>exit();</p>
<p>}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lifenlivin.com/2010/02/buggy-header-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<script type="text/javascript">var infolink_pid = 35265;</script>
<script type="text/javascript" src="http://resources.infolinks.com/js/infolinks_main.js"></script>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
try {
_uacct = "UA-4832193-1";
urchinTracker();
} catch(err) {}</script>

