<?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>tech stuff &#187; evil</title>
	<atom:link href="http://codytaylor.org/tag/evil/feed" rel="self" type="application/rss+xml" />
	<link>http://codytaylor.org</link>
	<description>From Cody Taylor.</description>
	<lastBuildDate>Sun, 30 Oct 2011 04:15:41 +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>Create A Simple Captcha With PHP and GD</title>
		<link>http://codytaylor.org/2009/08/create-a-simple-captcha-with-php-and-gd.html</link>
		<comments>http://codytaylor.org/2009/08/create-a-simple-captcha-with-php-and-gd.html#comments</comments>
		<pubDate>Wed, 12 Aug 2009 02:46:20 +0000</pubDate>
		<dc:creator>Cody Taylor</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[captcha]]></category>
		<category><![CDATA[evil]]></category>
		<category><![CDATA[gd]]></category>
		<category><![CDATA[png]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://codytaylor.org/?p=14213</guid>
		<description><![CDATA[Captchas are evil but really easy to implement.]]></description>
			<content:encoded><![CDATA[<p>Everyone hates captchas. I know I do. It sucks but they are a necessary evil. Most captchas are difficult (sometimes impossible) to read and they will consistantly drive visitors from your site. I&#8217;ve been on sites where the captcha was so hard to decipher that I&#8217;ve never gone back. </p>
<p>During the development of a new site I was forced to create one of these evil entities. It was surprisingly simple and painless. The captcha that I outline here is very, very simple and should never be used in production without a fair amount of tweaking. </p>
<p>Having developed my site using php I naturally turned to the GD library. </p>
<p>Firstly I generated a random string to display on the captcha :<br />
<pre><code>
//generate a random alphanumeric string of a specific length
function gen_random_string($length) 
{
&nbsp;&nbsp;$characters = &quot;0123456789ABCDEFGHIJKLMNOPQRSTUVWZYZ&quot;.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;abcdefghijklmnopqrstuvwxyz&quot;;
&nbsp;&nbsp;$real_string_length = strlen($characters) - 1;
&nbsp;&nbsp;for($p=0;$p&lt;$length;$p++)
&nbsp;&nbsp;{ $string .= $characters[mt_rand(0, $real_string_length)]; }
&nbsp;&nbsp;return $string;
}

//add to session array so other scripts can access it for the comparison
$_SESSION[&#039;image_text&#039;]= gen_random_string(8);

//split into character array - str_split only works in PHP 5
$text_array = str_split($_SESSION[&#039;image_text&#039;]);
</code></pre><br />
<br />
Now that I have my string I need to create the image. Make sure that GD is installed on your php apache server.<br />
<pre><code>
//create the image with a background image
$NewImage =imagecreatefrompng(&quot;bg1.png&quot;);
$cntr = 0;
foreach($text_array as $letter)
{
&nbsp;&nbsp;//generate a random color for each letter
&nbsp;&nbsp;$r = rand(0,200);
&nbsp;&nbsp;$g = rand(0,200);
&nbsp;&nbsp;$b = rand(0,200);
&nbsp;&nbsp;$textcolor = imagecolorallocate($NewImage, $r, $g, $b);

&nbsp;&nbsp;//random horizontal spacing
&nbsp;&nbsp;$spacing = (rand(5,10)+($cntr*10));
&nbsp;&nbsp;
&nbsp;&nbsp;//add the character to the image
&nbsp;&nbsp;imagestring($NewImage, 5, $spacing, rand(0,10), $letter, $textcolor);
&nbsp;&nbsp;$cntr++;
}
</code></pre><br />
<br />
The code above will generate a GD captcha image with random coloring and random horizontal and vertical spacing on top of a background image.<br />
Now we just need to output it.<br />
<pre><code>
//output the headers than the image as a PNG
header(&#039;Content-type: image/png&#039;);
header(&#039;Cache-Control: max-age=0&#039;);
header(&#039;Expires: &#039;.gmdate(&#039;r&#039;,time()-3600*24*365));
header(&#039;Pragma:&#039;);
ImagePNG($NewImage);
imagedestroy($NewImage);
</code></pre><br />
<br />
So if I called this file simple-captcha.php I could call it with this html:<br />
<pre><code>
&lt;img id=&#039;captcha&#039; name=&#039;captcha&#039; src=&quot;simple-captcha.php&quot;&gt;
</code></pre><br />
<br />
Here is what it looks like : </p>
<p></p>
<table cellpadding=0 cellspacing=0>
<tr>
<td>
<img id='captcha' name='captcha' src="http://codytaylor.org/simple-captcha.php">
</td>
<td>
<input type='button' value='refresh captcha example' onclick='refresh_captcha();'>
</td>
<td>
</table>
<p>
This captcha can be strengthened by using other <a href='http://ca2.php.net/manual/en/ref.image.php' title='PHP GD image functions'>GD image functions</a> such as imagefilledellipse and imageline to create artifacts in the background. It also helps to use different fonts and to change the angle of the letters.<br />
<script type='text/javascript'>
function refresh_captcha()
{ document.getElementById("captcha").src = 'http://codytaylor.org/simple-captcha.php?asdf='+Math.random(); }
</script></p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcodytaylor.org%2F2009%2F08%2Fcreate-a-simple-captcha-with-php-and-gd.html&amp;linkname=Create%20A%20Simple%20Captcha%20With%20PHP%20and%20GD"><img src="http://codytaylor.org/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://codytaylor.org/2009/08/create-a-simple-captcha-with-php-and-gd.html/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

