Captchar - a simple Captcha in PHP
Captchas are images frequently used on web pages to preven the access of automated robots to web pages. The generally consist of an image of text, distorted such that a computer cannot read it, but a human can. The user is then required to type the text in the image into a box, and the web server checks to see if the letters were typed correctly. This is an ideal implementation; however, programmers are constantly trying to beat automated reading robots while trying to have the images remain readable by actual humans.
Captchar is a simple captcha written in PHP. It requires the GD library and PHP 5 to run. Here's how it looks like:
Show me another
Test it out:
Strengths of this implementation include the light weight and ability to use a three methods of captcha generation: random characters, a wordlist and a fake-word generator based on an analysis of letter pair frequencies (seen above). It is also relatively difficult for computers to read compared to similar free implementations found on the internet.
Some other features include error handling, only easy-to-read letters and words, random color generation, and image distortion.
Download Version 1.2.1
You can download the source (txt, 5KB) and put it on your webserver and try it out yourself. You may also want the default worldlist (txt, 39KB) and the following font files: font.ttf (268KB) and fonta.ttf (59KB).
You're also free to distribute and use this code under the MIT license (included in source file). Enjoy!
Implementation
Using it is really simple. Download captcha.php and put it somewhere. To put it on any page,
Just use a standard html <img> tag like the following:
<img src="captcha.php" alt="Captcha Image" />
You'll need to include an HTML form to get and validate user input. The captcha value is stored as
$_SESSION['captcha'], so you might use something like this to validate user input:
if($_SESSION['captcha'] == $user_input && $_SESSION['captcha']) {
//Captcha input is valid, do things here.
unset($_SESSION['captcha']); //This line is really important, do not forget it
}
else {
//Captcha input is invalid, do things here.
}
Version History
Version 1.0 (August 20, 2010) - initial release.
Version 1.1 (May 22, 2010) - added relatively simple image distorion, explaination of the magic numbers by giving them variable names (configuration is now much easier).
Version 1.2 (May 25, 2010) - added a new random word generator that makes semi-plausible English words based on bigram frequencies, improved distortion randomness, removed the obsuring lines by default.
Version 1.2.1 (May 26, 2010) - Fixed bug where letters may sometimes not show up.