<?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>Dashifen.com &#187; Programming</title>
	<atom:link href="http://dashifen.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://dashifen.com</link>
	<description>The online musings of an unrepentant geek.</description>
	<lastBuildDate>Wed, 18 Jan 2012 23:30:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Avoid Duplicate Submissions with SHA1</title>
		<link>http://dashifen.com/avoid-duplicate-submissions-with-sha1/</link>
		<comments>http://dashifen.com/avoid-duplicate-submissions-with-sha1/#comments</comments>
		<pubDate>Tue, 05 Jul 2011 19:03:58 +0000</pubDate>
		<dc:creator>David Dashifen Kees</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[anonymous functions]]></category>
		<category><![CDATA[anti-duplication]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[verification]]></category>

		<guid isPermaLink="false">http://dashifen.com/?p=228</guid>
		<description><![CDATA[I had actually forgotten that I had done this in a project that I worked on a few months ago but came across it again today.  I figured I&#8217;d blog about it for a moment.  Not because I think it likely that too many of you, dear readers, will care deeply about my PHP tricks, [...]]]></description>
			<content:encoded><![CDATA[<p>I had actually forgotten that I had done this in a project that I worked on a few months ago but came across it again today.  I figured I&#8217;d blog about it for a moment.  Not because I think it likely that too many of you, dear readers, will care deeply about my PHP tricks, but in the hopes that I&#8217;ll remember the technique and be able to find it here if I need it again in the future.</p>
<p><span id="more-228"></span></p>
<p>So here&#8217;s the scenario: you want to be sure that a visitor doesn&#8217;t submit the same information through a web form multiple times.  Maybe you&#8217;re worried about them refreshing the page, for example.  Regardless of the circumstances, all you have is the data they submitted and you need a quick way to try and make sure it doesn&#8217;t match earlier data.  The way I recently did that was to use an anonymous function, a handy feature that shipped with PHP 5.3 if I remember correctly, to reduce the date posted to the server to a string and then hash it.  It looked like this:</p>
<pre style="padding-left: 30px;">$hash = sha1(array_reduce($values, function($reduction, $value) { return $reduction.$value; }));</pre>
<p>And that&#8217;s it.  Here&#8217;s what&#8217;s going on though:</p>
<ol>
<li>The <a href="http://us3.php.net/manual/en/function.array-reduce.php">array_reduce</a> function takes an array and a callback function as parameters and returns a string.</li>
<ul>
<li>Before we had anonymous functions you needed to use <a href="http://us3.php.net/manual/en/function.create-function.php">create_function</a> or define your function elsewhere in your code.  But with anonymous functions, you can just jam it right in there.</li>
<li>That callback takes two parameters; I&#8217;ve named them $reduction and $value.  That callback is called as the array is walked and, in the code above, simply concatenates all of the array values into one long string.</li>
<li>That string is then the return value from the function.</li>
</ul>
<li>The <a href="http://us3.php.net/manual/en/function.sha1.php">sha1</a> function then takes that string and makes a hash out of it.</li>
</ol>
<p>I suppose you could just leave the string as a string, but in case you need to jam the information into a database sometimes it&#8217;s nice to know exactly the size of the data you&#8217;re about to insert or update to optimize your table. A cursory glance at the <a href="http://us3.php.net/manual/en/ref.array.php">PHP array functions</a> didn&#8217;t seem to indicate that my code above duplicates something that PHP could do for me out of the box, so I&#8217;m pretty pleased with it.</p>
<p>How does this prevent duplication?  Well if you have a record of these hashes &#8212; in a database or in the Session or whatever &#8212; you can compare the hash for new information against one or more of the hashes for the old and if you find a match you can be pretty sure that you&#8217;ve got a duplicate.  Sure, it&#8217;s possible to collide but <a href="http://en.wikipedia.org/wiki/SHA-1">Wikipedia</a> tells me it&#8217;s pretty unlikely to happen.</p>
<p>Hope it&#8217;s useful to you.  And if you&#8217;re me, then thanks me!</p>
]]></content:encoded>
			<wfw:commentRss>http://dashifen.com/avoid-duplicate-submissions-with-sha1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

