CAPTCHA Adapters

The following adapters are shipped with Zend Framework by default.

Zend_Captcha_Word

Zend_Captcha_Word is an abstract adapter that serves as the base class for most other CAPTCHA adapters. It provides mutators for specifying word length, session TTL, the session namespace object to use, and the session namespace class to use for persistence if you do not wish to use Zend_Session_Namespace. Zend_Captcha_Word encapsulates validation logic.

By default, the word length is 8 characters, the session timeout is 5 minutes, and Zend_Session_Namespace is used for persistence (using the namespace "Zend_Form_Captcha_<captcha ID>").

In addition to the methods required by the Zend_Captcha_Adapter interface, Zend_Captcha_Word exposes the following methods:

  • setWordLen($length) and getWordLen() allow you to specify the length of the generated "word" in characters, and to retrieve the current value.

  • setTimeout($ttl) and getTimeout() allow you to specify the time-to-live of the session token, and to retrieve the current value. $ttl should be specified in seconds.

  • setUseNumbers($numbers) and getUseNumbers() allow you to specify if numbers will be considered as possible characters for the random work or only letters would be used.

  • setSessionClass($class) and getSessionClass() allow you to specify an alternate Zend_Session_Namespace implementation to use to persist the CAPTCHA token and to retrieve the current value.

  • getId() allows you to retrieve the current token identifier.

  • getWord() allows you to retrieve the generated word to use with the CAPTCHA. It will generate the word for you if none has been generated yet.

  • setSession(Zend_Session_Namespace $session) allows you to specify a session object to use for persisting the CAPTCHA token. getSession() allows you to retrieve the current session object.

All word CAPTCHAs allow you to pass an array of options to the constructor, or, alternately, pass them to setOptions(). You can also pass a Zend_Config object to setConfig(). By default, the wordLen, timeout, and sessionClass keys may all be used. Each concrete implementation may define additional keys or utilize the options in other ways.

Note: Zend_Captcha_Word is an abstract class and may not be instantiated directly.

Zend_Captcha_Dumb

The Zend_Captch_Dumb adapter is mostly self-descriptive. It provides a random string that must be typed in reverse to validate. As such, it's not a good CAPTCHA solution and should only be used for testing. It extends Zend_Captcha_Word.

Zend_Captcha_Figlet

The Zend_Captcha_Figlet adapter utilizes Zend_Text_Figlet to present a figlet to the user.

Options passed to the constructor will also be passed to the Zend_Text_Figlet object. See the Zend_Text_Figlet documentation for details on what configuration options are available.

Zend_Captcha_Image

The Zend_Captcha_Image adapter takes the generated word and renders it as an image, performing various skewing permutations to make it difficult to automatically decipher. It requires the ยป GD extension compiled with TrueType or Freetype support. Currently, the Zend_Captcha_Image adapter can only generate PNG images.

Zend_Captcha_Image extends Zend_Captcha_Word, and additionally exposes the following methods:

  • setExpiration($expiration) and getExpiration() allow you to specify a maximum lifetime the CAPTCHA image may reside on the filesystem. This is typically a longer than the session lifetime. Garbage collection is run periodically each time the CAPTCHA object is invoked, deleting all images that have expired. Expiration values should be specified in seconds.

  • setGcFreq($gcFreq) and getGcFreg() allow you to specify how frequently garbage collection should run. Garbage collection will run every 1/$gcFreq calls. The default is 100.

  • setFont($font) and getFont() allow you to specify the font you will use. $font should be a fully qualified path to the font file. This value is required; the CAPTCHA will throw an exception during generation if the font file has not been specified.

  • setFontSize($fsize) and getFontSize() allow you to specify the font size in pixels for generating the CAPTCHA. The default is 24px.

  • setHeight($height) and getHeight() allow you to specify the height in pixels of the generated CAPTCHA image. The default is 50px.

  • setWidth($width) and getWidth() allow you to specify the width in pixels of the generated CAPTCHA image. The default is 200px.

  • setImgDir($imgDir) and getImgDir() allow you to specify the directory for storing CAPTCHA images. The default is "./images/captcha/", relative to the bootstrap script.

  • setImgUrl($imgUrl) and getImgUrl() allow you to specify the relative path to a CAPTCHA image to use for HTML markup. The default is "/images/captcha/".

  • setSuffix($suffix) and getSuffix() allow you to specify the filename suffix for the CAPTCHA image. The default is ".png". Note: changing this value will not change the type of the generated image.

  • setDotNoiseLevel($level) and getDotNoiseLevel(), along with setLineNoiseLevel($level) and getLineNoiseLevel(), allow you to control how much "noise" in the form of random dots and lines the image would contain. Each unit of $level produces one random dot or line. The default is 100 dots and 5 lines. The noise is added twice - before and after the image distortion transformation.

All of the above options may be passed to the constructor by simply removing the 'set' method prefix and casting the initial letter to lowercase: "suffix", "height", "imgUrl", etc.

Zend_Captcha_ReCaptcha

The Zend_Captcha_ReCaptcha adapter uses Zend_Service_ReCaptcha to generate and validate CAPTCHAs. It exposes the following methods:

  • setPrivKey($key) and getPrivKey() allow you to specify the private key to use for the ReCaptcha service. This must be specified during construction, although it may be overridden at any point.

  • setPubKey($key) and getPubKey() allow you to specify the public key to use with the ReCaptcha service. This must be specified during construction, although it may be overridden at any point.

  • setService(Zend_Service_ReCaptcha $service) and getService() allow you to set and get the ReCaptcha service object.

  • isValid($value, $context = null) validates the user's input against the ReCaptcha service. $value is an array with the following keys:

    • recaptcha_challenge_field - Value of the ReCaptcha widget's challenge field (hidden)

    • recaptcha_response_field - Value of the ReCaptcha widget's user input field

blog comments powered by Disqus