Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

CGI redirection

by 5plit_func (Beadle)
on Jan 17, 2013 at 13:46 UTC ( #1013782=perlquestion: print w/replies, xml ) Need Help??

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

Good day all i am learning CGI using perl and i developed simple script that redirect a user based on form selection to a given website. the code follows below.

if($query->param('op') eq "ds") { print $query->redirect($query->param('website')); }else { print" <html><head><title>Web redirection</title></head> <body> <form action=\"\" method=\"post\" > <fieldset> <legend>Kindly select a website to visit</legend> <strong>Select a website form the list:</strong> <select name=\"website\"> <option value=\"\"></option> <option value=\"\"></option> <option value=\"\"></option> </select> <input type=\"hidden\" name=\"op\" value=\"ds\" > <input type=\"submit\" name=\"submit\" value=\"Browse the web\"> </form> </body> </html>"; }

The issue here is the code does not redirect the user to the selected website it gives this respond code on the browser.

Status: 302 Found Location:

Please i kindly need your assistance and comments thanks in advance.

Replies are listed 'Best First'.
Re: CGI redirection
by tobyink (Canon) on Jan 17, 2013 at 14:20 UTC

    Did you print an HTTP header before the redirect?

    Don't do that.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      Many thanks tobyink it was the header which was first printed in the body of the documents right at the top of the program so i moved it into the if statement which needed it and it worked. thanks.
Re: CGI redirection
by NetWallah (Canon) on Jan 17, 2013 at 16:13 UTC
    The CGI module documentation on redirect says:
    You should always use full URLs (including the http: or ftp: part) in redirection requests. Relative URLs will not work correctly.
    In your case, the entry for "" is missing the "http://".

                 Most people believe that if it ain't broke, don't fix it.
            Engineers believe that if it ain't broke, it doesn't have enough features yet.

Re: CGI redirection
by Anonymous Monk on Jan 17, 2013 at 14:25 UTC
    By far the best way to debug a problem like this is to use the debugging facilities of your browser ... such as the Firebug plugin, or turning on the developer menu in others ... and use it to look at exactly what is being sent and received when you go somewhere that achieves a successful redirect.   Then, look at your own site and compare the two.
Re: CGI redirection
by 7stud (Deacon) on Jan 18, 2013 at 00:33 UTC

    Also, if you find yourself escaping lots of characters inside a string, you are not taking advantage of perl's numerous quoting mechanisms. Look here:

    my $html = <<END_OF_HTML; <html> <head><title>Web redirection</title> </head> <body> <form action="" method="post" > <fieldset> <legend>Kindly select a website to visit</legend> <strong>Select a website form the list:</strong> <select name="website"> <option value=""></option> <option value=""></option> <option value=""></option> </select> <input type="hidden" name="op" value="ds" > <input type="submit" name="submit" value="Browse the web"> </form> </body> </html> END_OF_HTML print $html;

    That's called the 'here doc' syntax. Perl also has q{} and qq{}:

    use strict; use warnings; use 5.012; my $greeting = 'hello'; my $str1 = q{The lady said $greeting to me.}; my $str2 = qq{The lady said $greeting to me}; say $str1; say $str2; --output:-- The lady said $greeting to me. The lady said hello to me.

    q{} is an alternative to single quotes, and qq{} is an alternative to double quotes--and you can use any delimiter you want:

    say q['hello' {world}]; say q*hello ["'] world*; --output:-- 'hello' {world} hello ["'] world
Re: CGI redirection
by alpha (Scribe) on Jan 18, 2013 at 08:31 UTC
    Don't use, use a web framework (Dancer, Catalyst, Mojolicious, etc). Don't mix HTML and Perl code, use a templating system.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1013782]
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2020-09-29 21:15 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (153 votes). Check out past polls.