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

I keep getting a 404 error. I have created a form. This form is to redirect (after hitting the submit button) to another page letting the user know they need to go back. I am trying to redirect to the c12case1b.html file but am apparently putting in the wrong extension? They are on the server I am using but keep getting a 404 error. Here is all three files...
<!DOCTYPE html> <html> <body> <FORM METHOD="POST" ACTION="/~zach3327/cgi-bin/c12case1.cgi"> Phoenix Industries:<br> <name="phoenix industries"> <br> Name:<br> <input type="text" name="name" value=> <br> Address:<br> <input type="text" name="address" value=> <br> City:<br> <input type="text" name="city" value=> <br> State:<br> <input type="text" name="state" value=> <br> ZIP code:<br> <input type="text" name="zip code" value=> <br> <br><br> <input type="submit" value="Submit"> <input type="reset" value="Reset" +> </form> </body> </html>
<HEAD><TITLE>Phoenix Industries</TITLE></HEAD> <BODY> <H1 ALIGN=center>Phoenix Industries<HR> Please press your browser’s back button to return to the form +. Then complete all items
#!/usr/bin/perl #c12case1.cgi -Location header use CGI qw(:standard); use strict; #declare and assign value to variable my ($name); $name = param('Name'); #create Hello page if($name ne "") { print "Content-type: text/html\n\n"; print "<HTML>\n"; print "<HEAD><TITLE>Phoenix Industries</TITLE></HEAD\n"; print "<BODY>\n"; print "<H1 ALIGN=center>Phoenix Industries<HR>\n"; print "Please return to your browser's back button to return to t +he form, $name!</H1></BODY></HTML>"; } else { print "Location: http://~zach3327/public_html/chapter12/c12case1b.html\n\n"; }

Replies are listed 'Best First'.
Re: What am I doing wrong here?
by haukex (Archbishop) on Apr 12, 2018 at 06:55 UTC

    The main thing is probably the URL "http://~zach3327/public_html/chapter12/c12case1b.html": ~zach3327 is probably not the hostname. Try removing the http:// and starting the URL with just /~zach3327/... instead (you could also get the script's location in different formats via CGI.pm's url function, and if you need further URL manipulation, see URI - Update: see my other post). It also seems that you've embedded hard newlines in the string as "Location:\n\t    ..." - I'd recommend not doing that, i.e. "print "Location: /~zach3327/...".

    I also see a few other issues:

    • Note the casing: <input type="text" name="name" vs. param('Name')
    • You're not using warnings, which would help you catch other potential problems if they arise.
    • Your style of HTML is fairly old, and contain at least one typo that I see right off the bat (...</HEAD\n"). If you are going from a tutorial of some kind, I think it's a pretty old one. That'd also explain the use of CGI.pm, which isn't really modern anymore. See also UP-TO-DATE Comparison of CGI Alternatives, CGI Parameters, and CGI::Alternatives.
    • By printing out $name as part of the URLHTML without escaping it, you are exposing yourself to a Cross-site scripting (XSS) attack (tutorial). You should at least use escapeHTML from CGI::HTML::Functions.
    • You are printing some things yourself that you could be using CGI.pm's functionality for: see its header and redirect functions.

    Edit: Fixed thinko.

Re: What am I doing wrong here?
by haj (Vicar) on Apr 12, 2018 at 08:46 UTC
    Your Value for Location has ~zach3327 in the place where the name of the web server (the "host") is expected. If you don't want to hardcode the name of your webserver, then use CGI's methods server_name or virtual_host, whichever is appropriate.
      use CGI's methods server_name or virtual_host

      That's a start, but unfortunately not enough: it misses the protocol and port. I think it's better to use CGI.pm's url function/method. For example, also demonstrating URI:

      #!/usr/bin/env perl use warnings; use strict; use CGI qw/:standard/; use URI; print header("text/plain"); print server_name(), "\n"; print virtual_host(), "\n"; print url(), "\n"; my $url = URI->new( url( -base => 1 ) )->canonical; $url->path( '/some/new/path' ); print "$url\n";

      Would output, for example:

      www.example.com www.example.com http://www.example.com:8080/cgi-bin/test.cgi http://www.example.com:8080/some/new/path