Captcha.pm is a Perl module which provides a simple CAPTCHA framework. It's written in a object oriented programming style. Session information is stored in a hashed DB file (/var/tmp/captcha.db by default).
From its inline documentation:
#!/usr/bin/perl -w use strict; use warnings; use FindBin qw($Bin); use lib $Bin . "/../lib"; use Captcha; new Captcha()->image();
#!/usr/bin/perl -w use strict; use warnings; use CGI; use FindBin qw($Bin); use lib $Bin . "/../lib"; use Captcha; my $cgi = new CGI(); my $captcha = new Captcha(); if (!$captcha->ok($cgi->param("captcha"))) { # user entered a wrong captcha code -> print an error exit -1; } # do the real work, e.g. place the order, do some heavy computation, etc. ...
<html> <head> <title>Place an order</title> </head> <body> <form method="POST" action="/cgi-bin/order.pl"> ... some other input fields ... <div> <p>Please solve the following equation:</p> <img src="/cgi-bin/captcha.pl"> = <input type="text" name="captcha" size="4"> </div> <input type="submit"> </form> </body> </html>
#!/usr/bin/perl -w use strict; use warnings; use CGI; use FindBin qw($Bin); use lib dirname($Bin) . "/../lib"; use Captcha; my $cgi = new CGI(); my $captcha = new Captcha(); # ... print html form ... print $captcha->link(); # ... print reminder of html form ...
You can override builtin defaults by providing the constructor one or more of these parameters:
Parameter | Default | Info |
---|---|---|
dbfile | /var/tmp/captcha.db | Location of the session database file |
cookie | captcha_sid | Name of the session cookie |
width | 200 | Width of the CAPTCHA image in pixels |
height | 50 | Height of the CAPTCHA image in pixels |
permitted_ips | [] | List of IP addresses which are allowed without any CAPTCHA code; simple string-like prefix match, e.g. "192.168." matches 192.168.0.0/16 |
bypass_func | undef | Reference to a (anonymous) function which will bypass CAPTCHA checking if it returns a true value |
captcha_url | captcha.pl | URL of the CGI script which prints the CAPTCHA image |
captcha_field | captcha | Name of the form field which contains the CAPTCHA code entered by the user |
Download Captcha.pm, captcha.pl, and captcha_test.pl from below. Save them to the cgi-bin folder on your webserver. Point your webbrowser to /cgi-bin/captcha_test.pl on your webserver.