View on
MetaCPAN
John Drago > ASP4x-Captcha-Imager > ASP4x::Captcha::Imager

Download:
ASP4x-Captcha-Imager-0.003.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  2
Open  0
View/Report Bugs
Module Version: 0.003   Source  

NAME ^

ASP4x::Captcha::Imager - Imager-based CAPTCHA for your ASP4 web application.

SYNOPSIS ^

In Your asp4-config.conf

  {
    ...
    "system": {
      ...
      "settings": {
        ...
        "captcha_key":      "Some random string of any length",
        "captcha_font":     "@ServerRoot@/etc/LiberationSans-Regular.ttf",
        "captcha_width":    140,
        "captcha_height":   40,
        "captcha_bg_color": "FFFFFF",
        "captcha_length":   4
        ...
      }
    }
  }

In a handler

Simply subclass ASP4x::Captcha::Imager as shown below:

  package dev::captcha;

  use strict;
  use warnings 'all';
  use base 'ASP4x::Captcha::Imager';
  use vars __PACKAGE__->VARS;

  1;# return true:

In your ASP Script:

Render the Captcha image:

  <html>
  <head>
  <style type="text/css">
  LABEL {
    display:        block;
    width:          265px;
    text-align:     right;
    float:          left;
    padding-right:  5px;
  }
  
  IMG {
    border: dotted 1px #AAA;
  }
  </style>
  </head>
  <body>
    <form action="/handlers/dev.validate" method="post">
      <p>
        <label>Enter the code you see below:</label>
        <input type="text" name="security_code" />
      </p>
      <p>
        <label>&nbsp;</label>
        <img id="captcha" src="/handlers/dev.captcha?r=<%= rand() %>" alt="Security Code" />
        <a href="" onclick="document.getElementById('captcha').src = '/handlers/dev.captcha?r=' + Math.random(); return false">
          (Click for a new Image)
        </a>
      </p>
      <p>
        <label>&nbsp;</label>
        <input type="submit" value="Submit" />
      </p>
    </form>
  </body>
  </html>

Validate the Captcha

  package dev::validate;

  use strict;
  use warnings 'all';
  use base 'ASP4::FormHandler';
  use vars __PACKAGE__->VARS;

  sub run
  {
    my ($s, $context) = @_;
    
    my $secret = $Config->system->settings->captcha_key;
    my $code = lc($Form->{security_code});
    
    # It should exist in the session and have the correct value:
    if( exists($Session->{asp4captcha}->{$code}) )
    {
      # Ding ding ding ding ding!
      $Response->Write("CORRECT");
    }
    else
    {
      # Bzzzzzzzzzzt: WRONG!
      $Response->Write("WRONG");
    }# end if()
  }# end run()

  1;# return true:

DESCRIPTION ^

"CAPTCHA" is the little security image containing a hard-to-read code that you may have seen on some websites. They are common on sign-up forms and email forms. The idea is that a bot or script can't read the image and can't guess the code.

ASP4x::Captcha::Imager uses Imager to generate an image of a random string of numbers and letters.

What Does the Captcha Image Look Like?

You can see an example in the example/example.png file included with this distribution.

Recommendations and Considerations

What About Fonts?

Because Linux systems tend to put fonts in several different places, I recommend copying the font file (*.ttf) into the etc/ folder of your website and referencing it (just like you see in the t/ folder of this distribution and in the SYNOPSIS example above.

Mono-space fonts are recommended over variable-width fonts. So, "Courier New" is recommended over Verdana.

SEE ALSO ^

ASP4, Imager

PREREQUISITES ^

Imager, ASP4, Digest::MD5

AUTHOR ^

John Drago <jdrago_999@yahoo.com>

LICENSE ^

This software is Free software and may be used and redistributed under the same terms as any version of Perl itself.

syntax highlighting: