<?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>newthink.net &#187; PHP</title>
	<atom:link href="http://www.newthink.net/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.newthink.net</link>
	<description>Less Talk. More Do.</description>
	<lastBuildDate>Tue, 26 Jan 2010 18:28:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Solving The HTTP Double-Post Dilema</title>
		<link>http://www.newthink.net/2007/03/06/solving-the-double-post-dilema/</link>
		<comments>http://www.newthink.net/2007/03/06/solving-the-double-post-dilema/#comments</comments>
		<pubDate>Wed, 07 Mar 2007 03:20:03 +0000</pubDate>
		<dc:creator>Ash Christopher</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://newthink.net/blog/2007/03/06/solving-the-double-post-dilema/</guid>
		<description><![CDATA[Everyone who has done any sort of web development, and made use of the HTTP POST has run into this problem. If you haven&#8217;t, are you sure you are doing proper QA?
Basically, the problem comes from having the POST information still resident after an HTTP POST. If the user were to refresh their browser, that [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone who has done any sort of web development, and made use of the <strong>HTTP POST</strong> has run into this problem. If you haven&#8217;t, are you sure you are doing proper QA?</p>
<p>Basically, the problem comes from having the POST information still resident after an <strong>HTTP POST</strong>. If the user were to refresh their browser, that POST information will be resent, resulting in a double post. There are many ways to solve this problem, but the one I will outline (in PHP) seemed like the best for my situation.</p>
<p>Let us assume we have a text input and a button and that our form.php file will process our form as well as present it to the user (yes, I know this isn&#8217;t a good idea, but it is just an example).<br />
<span id="more-14"></span></p>
<blockquote><p>
&lt;form method=&#8221;post&#8221; action=&#8221;form.php&#8221;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;input name=&#8221;mytext&#8221; /&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;input name=&#8221;submitBtn&#8221; type=&#8221;submit&#8221; /&gt;<br />
&lt;/form&gt;
</p></blockquote>
<p>When we load the page, we are going to check whether the <strong>$_POST</strong> variable is set. If it is, then we are going to do something with it.</p>
<blockquote><p>
session_start();</p>
<p>if(isset($_POST['mytext']))<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;saveToDatabase($_POST['mytext']);<br />
}
</p></blockquote>
<p>Now that we are doing something with this <strong>$_POST['mytext']</strong> variable, we can see the double-post problem materialize. Since the <strong>HTTP POST</strong> is still populated, when the user presses refresh on their browser, the <strong>$_POST['mytext']</strong> will be saved to the database twice.</p>
<p>What we need to do is get rid of the <strong>$_POST</strong> variable after we are done using it.</p>
<p>At the beginning of the form.php file I am going to create a server-side session to store any post information we get. Then I have a check to see whether the $_POST array has been populated.</p>
<p>If the <strong>$_POST</strong> has been populated, we are going to store the post information in the server-side session we have started and we send a blank header back out (which removes the <strong>HTTP POST</strong>).</p>
<blockquote><p>
if (isset($_POST['myText']))<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;$_SESSION['myText'] = $_POST['myText'];<br />
&nbsp;&nbsp;&nbsp;&nbsp;header(&#8220;Location: form.php&#8221;);<br />
}</p></blockquote>
<p>Now, we have gotten rid of the POST which removes the double-post problem, and have stored the information from the <strong>$_POST</strong> for use. We can then access the POST information stored in the session. Once we have copied the POST information from the session, we will unset the session so that the information doesn&#8217;t stay resident.</p>
<blockquote><p>
if (isset($_SESSION['myText']))<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;$myEnteredText = $_SESSION['myText'];<br />
&nbsp;&nbsp;&nbsp;&nbsp;unset($_SESSION['myText']);<br />
}
</p></blockquote>
<p>Now you can use the POST information without running the risk of a double post.</p>
<p>If you would like to see this tutorial in action, create a form.php file on your webserver, and paste the following:</p>
<blockquote><p>
&lt;?<br />
&nbsp;&nbsp;&nbsp;&nbsp;session_start();</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;if (isset($_SESSION['myText']))<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$myEnteredText = $_SESSION['myText'];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unset($_SESSION['myText']);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;if (isset($_POST['myText']))<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$_SESSION['myText'] = $_POST['myText'];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header(&#8220;Location: form.php&#8221;);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
?&gt;</p>
<p>&lt;html&gt;<br />
&lt;head&gt;</p>
<p>&lt;/head&gt;<br />
&lt;body&gt;</p>
<p>&lt;form action=&#8221;form.php&#8221; method=&#8221;POST&#8221;&gt;</p>
<p>&lt;?<br />
&nbsp;&nbsp;&nbsp;&nbsp;if (isset($myEnteredText))<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &#8220;&lt;p&gt;The message you entered was \&#8221;".$myEnteredText.&#8221;\&#8221;&lt;/p&gt;&#8221;;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
?&gt;</p>
<p>&lt;input name=&#8221;myText&#8221; type=&#8221;text&#8221; /&gt;<br />
&lt;input name=&#8221;submitBtn&#8221; type=&#8221;submit&#8221; value=&#8221;ok&#8221; /&gt;</p>
<p>&lt;/form&gt;<br />
&lt;/body&gt;</p>
<p>&lt;/html&gt;
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.newthink.net/2007/03/06/solving-the-double-post-dilema/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
