Thursday, October 31, 2013

Creating a Random Captcha with refresh Option

We all know that creating captcha is not a tough job in ColdFusion. But, one newbie in ColdFsion just asked me to help in creating a random captcha for his application.

I thought to share with others.

I have divided the entire code into three parts.

  1. captcha.cfm - Page where we will display the captcha image.
  2. Util.cfc - It contains a user defined function "generateRandomText" to generate random captcha text.
  3. refreshCaptcha.cfm - Page which will handle AJAX call while refreshing captcha image.

Lets see the "captcha.cfm" below:

Here, in above code I have used "ImageCreateCaptcha" function, which is added in ColdFusion 10 for captcha generation.

Lets see the "Util.cfc"

In this file, I have written a function called "generateRandomText"  for generating captcha string. Logic is very simple, you can modify it according to your requirement.

Next, come to "refreshCaptcha.cfm":

We are making AJAX call to this file which again generates random captcha image and sends to browser for display.

So, this is whole story for generating and refreshing captcha image in ColdFusion!!!

1 comment:

  1. That generateRandomText() function isn't really necessary. I just use createUUID() then reverse and grab the however many chars I need. I suppose I could just do a Right instead of a reverse and left. Anyway, the advantage of your algorithm is that you can control exactly which chars are in the random text. I like to exclude the numbers One and Zero and the letters L for Lima, I for Indigo and O for Oscar. The reason is the 1,L,I and O and 0 are too hard to tell apart, especially in a CAPTCHA. I mean, its not compulsory to use all 26 letters and all 10 digits, so why not reduce the confusion?

    My random text for CAPTCHAS generator is just this: