Captcha Library

The Captcha Library provides a means to generate a captcha. This library enables you to display a barrier-free captcha in the form of digits composed of non breaking spaces.

Initializing the Library

Like most other libraries in revIgniter, the Captcha Library is initialized in your controller using the loaderLoadLibrary handler:

loaderLoadLibrary "Captcha"

Example

Here is a simple example showing how to create a captcha in one of your controller methods:

loaderLoadLibrary "Captcha"

# GET CAPTCHA
put riCaptcha() into gData["captcha"]

# GET CAPTCHA CSS
put captchaStyle() into gData["captchaStyle"]

Note: Place the gData["captchaStyle"] variable to be merged in the header of your view file. This variable holds the style data for your captcha.

In order to prevent automated software from posting to your site you need to store a hash of the captcha digits, which is built from the captcha number and a key stored in your application/config/config.irev file, in a hidden field. Then you have to compare the posted data of the hidden field with a hash of the posted number. To retrieve the hash of the displayed captcha your code would look like this:

loaderLoadLibrary "Captcha"

# GET CAPTCHA
put riCaptcha() into gData["captcha"]

# GET CAPTCHA CSS
put captchaStyle() into gData["captchaStyle"]

#GET CAPTCHA HASH
put getCaptchaHash() into gData["captchaHash"]

Here is an example for captcha user input validation:

if validCaptchaUserInput(tInput, tHiddenField) is TRUE then
   get loadView("formSuccess")
end if

Where tInput is the posted captcha user input and tHiddenField is the captcha hash.

Captcha Configuraton

There are 4 different preferences available in your application/config/config.irev file. You can change them in the config file or you can set them manually as described below.

Explanation of Values:

Preferences are set by passing an array of preference values to the captcha initCaptcha handler. Here is an example of how you might set preferences:

loaderLoadLibrary "Captcha"

put 6 into tConfig["captchaLength"]
put "#FFFFFF" into tConfig["captchaBckgndColor"]
put "#646464" into tConfig["captchaColor"]

initCaptcha tConfig

Note:To change your secret key change the corresponding entry in the config file. Don't set it manually like other preferences shown above.

Captcha Method Reference

riCaptcha()

Call this function first to get a captcha:

put riCaptcha() into gData["captcha"]

captchaStyle()

Get captcha style tags to be placed in the header of the view file:

put captchaStyle() into gData["captchaStyle"]

getCaptchaHash()

Get hash from captcha number and a key. You may store this string in a hidden form field:

put getCaptchaHash() into gData["captchaHash"]

getCaptchaNum()

Get number the captcha is built with:

put getCaptchaNum() into gData["captchaNum"]

validCaptchaUserInput(pCaptchaInput, pHiddenFieldValue)

Validate captcha user input. The first parameter is the posted captcha user input and the second is the captcha hash retrieved from an hidden field. The function returns TRUE or FALSE.

if validCaptchaUserInput(tInput, tHiddenField) is TRUE then
   -- your code here
end if

initCaptcha pConfigArray

Used to set initial values whenever the library is loaded. The parameter is an array of captcha settings described above:

initCaptcha tConfig