I needed some form of a captcha thing for Share The Beat, just to put some token effort into preventing bot registrations, I tried to find ez-gimpy at captcha.net to no avail, so I used a PHP class from Pascal Rehfeldt. Integration with HTML_Quickform was way easier than I expected, but I thought I might document what I did for the novices out there.


1. Grab the captcha class from Pascal.

2. Either grab the font specified in the class, or point it at some other font (or some random font, you get the idea)

3. Try to load the example image, if it doesn't work turn off the headers in the class so you can see error messages (note that the TTF I downloaded didn't match the case of the one specified in the file, might want to look at that). Don't proceed untill that works.

4. Add the apropriate element to your form definition, mine looked like this:

$form->addElement('text', 'txtCaptcha','Crazy Text: ');

5. Register an additional rule for the text

$form->registerRule('captchaTest', 'function', 'checkCAPTCHA');

$form->addRule('txtCaptcha', 'Please ensure the text in the box matches the text in the image exactly', 'captchaTest');

6. Write that function:

function checkCAPTCHA ($name, $value)

{

if ($value == $_SESSION['CAPTCHAString'])

{

return true;

}else

{

return false;

}

}

7. Display the example as an image on your form (I use templates so this was easy)
<img src="./example.php">

8. Test

9. ..

10. Profit!



The class stores the captcha string in the session, so it is quite easy to access it later on. I personally saved the example.php file as captcha.png and told Apache to interpret it appropriately, your mileage may vary.


Things you could do to improve it:

-Select a random font from a list

-Make it case InSensiTIVe

-Find a more complicated captcha :-)

Comments »

No Trackbacks
No comments

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
 

Hi, I’m Paul Reinheimer, a developer working on the web.

I co-founded WonderProxy which provides access to over 200 proxies around the world to enable testing of geoip sensitive applications. We've since expanded to offer more granular tooling through Where's it Up

My hobbies are cycling, photography, travel, and engaging Allison Moore in intelligent discourse. I frequently write about PHP and other related technologies.

Search