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

Can someone explain why this code loads a 404 error (page not found) when I load this on a URL other than my own site? When I load my own site it processes just fine but when I try even http://www.google.com it redirects and says 404. The really interesting thing is the fact I'm not even redirecting! Something is going on when after I put in a URL in the form it tries to redirect to it in the browser if it's not the same as my domain.
#!/usr/bin/perl use CGI qw/:standard/; use CGI qw/:fatalsToBrowser/; use warnings; use strict; print header, start_html("test"); my $geturl = url_param('geturl'); my $commentid; if($geturl eq "") { &get_url; } else { &get_site; &print_form; } sub get_url { print qq(<form name="geturl" name="geturl" method="get" action="wp. +pl">); print qq(URL: <input type="text" name="geturl">); print qq(<input type="submit" name="submiturl"></form>); } sub print_form { print "test 1"; } sub get_site { use LWP::Simple; my $content = get($geturl); print "test"; }

Replies are listed 'Best First'.
Re: Odd LWP::Simple problem, unintentional redirect
by Khen1950fx (Canon) on Aug 31, 2011 at 07:09 UTC
    You had a "head" conflict between LWP::Simple and CGI. From the documentation:

    Note that if you are using both LWP::Simple and the very popular CGI.pm module, you may be importing a head function from each module, producing a warning like:

    "Prototype mismatch: sub main::head ($) vs none"
    Get around this problem by just not importing LWP::Simple's head function, like so:

    use LWP::Simple qw(!head); use CGI qw(:standard); # then only CGI.pm defines a head
    Then if you do need LWP::Simple's head function, you can just call it as:
    LWP::Simple::head($url)
    I ran your script like this:
    #!/usr/bin/perl use strict; use warnings; use LWP::Simple qw(!head); use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); head(); print header, start_html("test"); my $url = 'http://www.perlmonks.net'; my $geturl = url_param('geturl'); my $commentid; if ( defined( $_ = $geturl ) ) { &get_url; } else { &get_site; &print_form; } sub get_url { print qq(<form name="geturl" name="geturl" method="get" action="wp +.pl">); print qq(URL: <input type="text" name="geturl">); print qq(<input type="submit" name="submiturl"></form>); } sub print_form { print "test 1"; } sub get_site { my $content = get($geturl); print "test\n"; }

      You had a "head" conflict between LWP::Simple and CGI. From the documentation:

      And where, pray tell, does the OP use head in his code?

Re: Odd LWP::Simple problem, unintentional redirect
by duyet (Friar) on Aug 31, 2011 at 03:25 UTC

    The problem doesn't seem to be in this script (if it's complete) ... I ran it and things seem working ok.

    When an URL is given, another script is used: wp.pl. That would have done the redirect ...

Re: Odd LWP::Simple problem, unintentional redirect
by Anonymous Monk on Aug 31, 2011 at 03:32 UTC

    Can someone explain why this code loads a 404 error (page not found) when I load this on a URL other than my own site?

    Firewall? Proxy? ->dump the response and you'll get more info.