<?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>Ideologics &#187; Webdesign</title>
	<atom:link href="http://www.ideologics.co.uk/tags/webdesign/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ideologics.co.uk</link>
	<description>All About Computers</description>
	<lastBuildDate>Fri, 20 May 2011 04:05:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How to prevent multiple form submissions in PHP</title>
		<link>http://www.ideologics.co.uk/programming/how-to-prevent-multiple-form-submissions-in-php</link>
		<comments>http://www.ideologics.co.uk/programming/how-to-prevent-multiple-form-submissions-in-php#comments</comments>
		<pubDate>Tue, 30 Dec 2008 03:43:58 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Webdesign]]></category>

		<guid isPermaLink="false">http://www.eyeonsilicon.co.uk/?p=269</guid>
		<description><![CDATA[A problem that many programmers encounter is trying to prevent the user from submitting a form twice and inadvertently posting two sets of the same data &#8211; or in worse scenarios, charging a credit card twice! It&#8217;s a frustrating problem &#8230; <a href="http://www.ideologics.co.uk/programming/how-to-prevent-multiple-form-submissions-in-php">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A problem that many programmers encounter is trying to prevent the user from submitting a form twice and inadvertently posting two sets of the same data &#8211; or in worse scenarios, charging a credit card twice!</p>
<p>It&#8217;s a frustrating problem that is easily solved if you put your mind to it &#8211; ignoring it is nothing short of lazy.</p>
<p>On the client-side, we can use JavaScript to prevent the user from submitting the form more than once. Observe:</p>
<p><span id="more-269"></span><br />
<code>&lt;form action="script.php" method="post" onclick="validateForm();"&gt;<br />
Your name? &lt;input name="name" value=""&gt; &lt;input type="submit"&gt;<br />
&lt;/form&gt;<br />
&lt;script type="text/javascript"&gt;<br />
var submitted=false;<br />
function validateForm() {<br />
  if (submitted) {<br />
    alert("Please only submit the form once.");<br />
    return false;<br />
  } else {<br />
    return true;<br />
  }<br />
}<br />
&lt;/script&gt;</code></p>
<p>The idea is that when the user clicks the submit button, we flag the action in &#8216;submitted&#8217;. If the user clicks again, the function validateForm() will check &#8216;submitted&#8217; and prevent multiple submissions of the form.</p>
<p>But this isn&#8217;t enough &#8211; not everybody has JavaScript enabled, and assuming so would be lazy.</p>
<p><!--adsense--></p>
<p>We need some internal detection too, and one way to detect multiple submissions would be to use PHP sessions. Observe:</p>
<p><code>&lt;?php<br />
  session_start();<br />
  if ($_SESSION['formsessions'][$_POST['formsession']]) {<br />
    // form already submitted!<br />
    // ideally, at this point, you'd want to forward them to another page.<br />
    exit('form submitted twice.')<br />
  }<br />
// mark the session as submitted.<br />
  $_SESSION['formsessions'][$_POST['formsession']]=true;<br />
?&gt;</code></p>
<p>Again, we&#8217;re checking to see if the form has already been submitted. This script would require that we submit a &#8216;formsession&#8217; field with the form to make it uniquely identifiable. This could be as easy as inserting the following into the form code:</p>
<p><code>&lt;?php<br />
  echo '&lt;input type="hidden" name="formsession" value="'.md5(date('U').'-'.rand(1000000,9999999)).'"&gt;';<br />
?&gt;</code></p>
<p>With these two methods combined, no user should be able to submit the same form twice &#8211; accidentally, at least.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ideologics.co.uk/programming/how-to-prevent-multiple-form-submissions-in-php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

