Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

CGI form 'falls through' without capturing anything

by chexmix (Hermit)
on Jan 10, 2017 at 21:04 UTC ( [id://1179356]=perlquestion: print w/replies, xml ) Need Help??

chexmix has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Although I am grateful to still have hair remaining, I am pulling it out over an ostensibly simple CGI program I am trying to modify.

The interface keeps getting hit by bots and this results in cleanup time for yours truly -- so I was asked to put a simple CAPTCHA-type interface on this ca. 2000 or so CGI page.

I'm using the Authen::Captcha module to do this.

When the user is facing the front page s/he is asked to pick from a series of radio buttons - this is collected in a parameter 'CategoryRequest'. If this param is set, the program goes off and creates a new captcha object, then sends the user off to a page where this is displayed and a response is requested. Here's the code for that part, since it's the part causing me the headache:

sub getUserEntry { print header( "text/html" ), start_html( "User Validation" ), $header, h3( "Entry Validation" ), start_form, hr, p("Please enter the characters in the image at the bottom of t +he page"), textfield( -name => "UserEntry", -size => 15 ), br, br, submit( -name => "CodeValidation", -value => " Enter the Code " ), br, br, hr; print end_form; print "\<img src=\"./$www_output_dir/$md5sum.png\"\>"; print $footer; print end_html; return; }

... I'm expecting that sub to return to where I came from with the 'UserEntry' param filled in, so I can first validate the response and then make decisions based on the result ...

... but instead of waiting for the submit button to be pressed and the parameter to be duly defined, the program just swoops over or through the subroutine (whether I return things explicitly from it or not), assumes param( 'UserEntry' ) is empty, and displays both the UserEntry page along with an error page ("You haven't entered anything!") underneath it together in the same lump of html.

Why does this 'fall-through' happen? The logic I'm employing is as follows:

If the param( 'CategoryRequest' ) exists: create the captcha; get the user's response; If the user's response exists: <-- but this is never true check it against the real answer; do other things based on results of check

How do I get the UserEntry section to 'wait' for the user to enter something?

This feels like a far more basic question than I should have to be asking at this point in my learning, but ... here it is.

Thanks,

Glenn

Replies are listed 'Best First'.
Re: CGI form 'falls through' without capturing anything
by choroba (Cardinal) on Jan 10, 2017 at 21:31 UTC
    What part of the code implements "get the user's response"? Do you understand how HTTP and CGI work?

    The CGI code runs on the server and generates the page, the client (a browser) interprets that and shows it to a user. Running a subroutine on a server doesn't wait for user input. You need a different page for the Captcha, or implement it in JavaScript.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      "Do you understand how HTTP and CGI work?"

      This really is the key, of course. I didn't. When I started work on this I thought 'as long as I don't try to do anything the rest of the program isn't trying to do, I'll be okay', but that has turned out not to be the case at all.

      I queried a much more experienced friend on this and now have a better understanding of CGI; however, I am now uncertain how to solve the problem at all.

      I can make the desired 'captcha' image display correctly on the first of a number of screens. However, I don't know how to have the generated captcha object 'persist' to a 'place' where I can run the check of the user's guess against it: I can't shove it into a hidden field -- or at least I don't know how to correctly. So after the user hits submit on that first screen, I have the user's input in a parameter, but the generated captcha object is gone.

      I'd write more but I'm practicing succinctness. :)

      UPDATE: problem solved. It was a stupid problem, having nothing to do with making objects persist -- but everything to do with making my cool headspace persist so I don't miss stupid things. Thanks!!

Re: CGI form 'falls through' without capturing anything
by Anonymous Monk on Jan 10, 2017 at 21:43 UTC

    Here's the code for that part, since it's the part causing me the headache:

    Thats not the part you should be showing, all that does is show a form

    See cgi101

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1179356]
Approved by talexb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-03-28 18:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found