September 4, 2010
Home
|
Contact Us
are you a human?
Home
HIP Tests
Community
About Us
Contact Us
Perl SMS Example
#!/usr/bin/perl use strict; use CGI; use LWP::UserAgent; HandleRequest(); sub HandleRequest { # # All requests start here. If values are provided for the 'id' and 'code' # parameters, we assume the user is attempting to pass the CAPTCHA. If # the 'code' is not provided, it's assumed we need to present the user # with the form and CAPTCHA # my $Request = new CGI(); my $key = $Request->param('id'); my $code = $Request->param('code'); my $user = $Request->param('user'); if(defined $key && defined $code) { if(ValidateCode($key, $code)) { CreateAccount($user); } else { ShowEnterCodeForm("Invalid Code Entered"); } } else { ShowEnterCodeForm(""); } } sub GetData { my $url = shift; my $request = HTTP::Request->new(GET => $url); my $ua = LWP::UserAgent->new; my $response = $ua->request($request); return $response->content; } sub ValidateCode { # # The id and code sent via the ShowCodeForm end up here. # These values are used to make a request to the CAPTCHA system. # If the code matches, the CAPTCHA system returns 'success' # my $key = shift; my $code = shift; my $resp = GetData("https://areyouahuman.org/captcha.pl?id=$key&code=$codei&type=SMS2"); if($resp eq 'success') { return 1; } else { return 0; } } sub CreateAccount { my $user = shift; # create account $user my $content = "
Bogus account creation page
Account Created!
"; SendData($content); } sub SendData { my $content = shift; print "Content-type: text/html\n\n$content"; } sub ShowEnterCodeForm { # # This represnts a typical "create an account" form. # The portions of this form that are specific to the CAPTCHA # are the 'id' and 'code' fields. # my $msg = shift; my $key = GenerateRandomKey(); my $firstCode = GetData("https://areyouahuman.org/captcha.pl?id=$key&type=SMS2"); my $content = "
Bogus account creation page
Text 'VERIFY $firstCode' to 41411. In response to this, you will get a code. Enter that code below.
Username:
Enter Code:
$msg
"; SendData($content); } sub GenerateRandomKey { # # Generate a 32-byte alphanumeric ID that is passed to the CAPTCHA system # my $key = ""; my @alphabet = (); push(@alphabet, $_) for('a'..'z'); push(@alphabet, $_) for('A'..'Z'); push(@alphabet, $_) for('0'..'9'); while(length($key) < 32) { $key .= $alphabet[int(rand($#alphabet+1))]; } return $key; }