Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

CGI redirection

by 5plit_func (Acolyte)
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=\"redirect.pl\" method=\"post\" > <fieldset> <legend>Kindly select a website to visit</legend> <strong>Select a website form the list:</strong> <select name=\"website\"> <option value=\"www.sedocaonline.com\">Sedocaonline.com</option> <option value=\"http://www.yahoo.co.uk\">Yahoo.co.uk</option> <option value=\"http://www.play.com\">play.com</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: www.sedocaonline.com

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

Comment on CGI redirection
Select or Download Code
Re: CGI redirection
by tobyink (Abbot) 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 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 NetWallah (Abbot) 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 "www.sedocaonline.com" 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 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="redirect.pl" method="post" > <fieldset> <legend>Kindly select a website to visit</legend> <strong>Select a website form the list:</strong> <select name="website"> <option value="www.sedocaonline.com">Sedocaonline.com</option> <option value="http://www.yahoo.co.uk">Yahoo.co.uk</option> <option value="http://www.play.com">play.com</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 CGI.pm, use a web framework (Dancer, Catalyst, Mojolicious, etc). Don't mix HTML and Perl code, use a templating system.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2014-10-20 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (75 votes), past polls