I took a few moments today and updated my blog comments page to use the Alagad Captcha Component. The process was quite simple. Because I use a highly customized version of Ray Camden’s Blog.cfc I won’t get into many details.
In a nutshell, I placed the Captcha Component into the application scope at startup. When a user accesses my comments page I call CreateCaptcha() to generate the Captcha image and related hash. This method returns a structure of information on the generated Captcha. I’ve elected to store the file name for the generated image and the hash in the user session.
The Captcha image is displayed on the comments form using an image tag pointing to a file named captchaImage.cfm. This file uses cfcontent to display and then delete the image based on the file name stored in session. A form field is displayed at this point to for the user to type in the text displayed.
When the user submits the form I again use the Captcha component stored in the application scope. This time I call it’s validate() method and pass in the hash stored in the session and the text provided by the user. If these match I know a human has filled out the form. If these do not match I send the user back to the form and ask them to try again.