File: captcha.pl

package info (click to toggle)
libcaptcha-recaptcha-perl 0.98%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, trixie
  • size: 176 kB
  • sloc: perl: 274; makefile: 2
file content (75 lines) | stat: -rwxr-xr-x 1,436 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/perl
# Simple CGI Captcha

use strict;
use warnings;
use Captcha::reCAPTCHA;
use CGI::Simple;

# Your reCAPTCHA keys from
#  https://www.google.com/recaptcha/admin/create
# Googles Test signature
use constant PUBLIC_KEY  => '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI';
use constant PRIVATE_KEY => '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe';

$| = 1;

my $q = CGI::Simple->new;
my $c = Captcha::reCAPTCHA->new;

my $error = undef;

print "Content-type: text/html\n\n";
print <<EOT;
<html>
  <body>
    <form action="" method="post">
EOT

# Check response
if ( $q->param( 'recaptcha_response_field' ) ) {
  my $result = $c->check_answer(
    PRIVATE_KEY, $ENV{'REMOTE_ADDR'},
    $q->param( 'recaptcha_challenge_field' ),
    $q->param( 'recaptcha_response_field' )
  );

  if ( $result->{is_valid} ) {
    print "Yes!";
  }
  else {
    $error = $result->{error};
  }
}


if ( $q->param( 'g-recaptcha-response' ) ) {
   my $result = $c->check_answer(
     PRIVATE_KEY, $ENV{'REMOTE_ADDR'},
     $q->param( 'recaptcha_challenge_field' ),
     $q->param( 'recaptcha_response_field' )
   );

   if ( $result->{is_valid} ) {
     print "Yes!";
   }
   else {
     $error = $result->{error};
   }
 }

# Generate the form
print $c->get_html( PUBLIC_KEY, $error );

print "<h3>Version 2</h3>";

print $c->get_html_v2( PUBLIC_KEY );


print <<EOT;
    <br/>
    <input type="submit" value="submit" />
    </form>
  </body>
</html>
EOT