Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Perl CGI redirect

by aksusa (Initiate)
on Apr 15, 2013 at 21:25 UTC ( #1028778=perlquestion: print w/ replies, xml ) Need Help??
aksusa has asked for the wisdom of the Perl Monks concerning the following question:

Hi: I am developing a form which gets the server name from the user and it get added to url and when I hit the submit button I want to open the url into a new page The below script acutally changes the url based on the clientname input

FM=`date +'%m-%d-%Y' --date="-7 day"` TM=`date +'%m-%d-%Y'` URL="http://a.com:8080/bms/healthMonitor.do?method=redirect&operation= +BackupHistorySearch&subOperation=GetReport&clientName=$1&fromDateStr= +$FM&toDateStr=$TM" echo $URL

The front end cgi script is

#!/usr/bin/perl $data_file = '/var/tmp/backup.txt'; use CGI; use Fcntl; use Getopt::Long; use CGI::Carp qw( fatalsToBrowser ); use CGI qw( :standard ); $query = new CGI; unless ($action = $query->param('action')) { $action = 'none'; } print <<"EndOfText"; Content-type: text/html <HTML> EndOfText if ($action eq 'Search') { $comment = $query->param('comment'); open(FH, ">$data_file") or die "can't open $data_file: $!"; print FH $comment; close FH or die "can't close $data_file: $!"; } open (IN, "$data_file") or die "Can't open $data_file for reading: $!" +; while (<IN>) { @host = $_; $cmd = `bash /tmp/url.sh $host[0]`; #print "$cmd"; print redirect( "$cmd" ) } close IN or die "Can't close $data_file: $!"; print <<"EndOfText"; <A NAME="form"><H2>Add Logical:</H2></A> <FORM METHOD="POST" ACTION="n.cgi"> <TABLE> <TR> <TD ALIGN="right"><STRONG>Logical:</STRONG></TD> <TD> <TEXTAREA NAME="comment" ROWS=1 COLS=30 WRAP="virtual"></TEXTAREA> </TD> </TR> <TR><TD COLSPAN=2> </TD></TR> <TR> <TD> </TD> <TD><INPUT TYPE="submit" NAME="action" VALUE="Search"></TD> </TR> </TABLE> </FORM> </BODY> </HTML> EndOfText

When I print $cmd it prints the url on the screen. Not able to redirect to new window.. Any suggestions?

Comment on Perl CGI redirect
Select or Download Code
Re: Perl CGI redirect
by NetWallah (Monsignor) on Apr 16, 2013 at 03:22 UTC
    If you are doing a redirect, the redirect should be the ONLY THING sent to the client.

    In your case, you are printing the header before redirect.

    From the docs (emphasis mine):

    The redirect() method redirects the browser to a different URL. If you use redirection like this, you should not print out a header as well.

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

      Thanks for the reply. Where I can specify that ?

        The order of operations in the script ought to be something like this:

        1. Create the CGI object
        2. Check the params
        3. If the params are good for the redirect, do that and exit
        4. Otherwise, print your header and the form and maybe some error message if the params were invalid

        While you are there, you might also want to think about why you are using CGI twice, why you are using Getopt::Long and Fcntl and then apparently not referring to them, why you are not using strict and warnings, just how dangerous it is to shell out to a script located in /tmp and why you aren't running in taint mode.

Re: Perl CGI redirect
by poj (Curate) on Apr 16, 2013 at 18:11 UTC
    This script gives the user a selection which is more secure than a text box. If the URL shown is what you want then remove the hash from #Search and it should redirect.
    #!/usr/bin/perl # n.cgi use strict; use CGI qw( :standard ); use CGI::Carp qw( fatalsToBrowser ); use Date::Calc qw(Add_Delta_Days); # calc dates my ($d,$m,$y) = (localtime)[3,4,5]; $y+=1900; $m+=1; my ($y0,$m0,$d0) = Add_Delta_Days($y,$m,$d,-7); my $FM = sprintf "%02d-%02d-%04d",$m0,$d0,$y0; my $TM = sprintf "%02d-%02d-%04d",$m,$d,$y; # page layout my $page =<< "EndOfText"; <h2>Add Logical:</h2> <form name="form" method="post" action="n.cgi"> <table> <tr> <td align="right"><strong>Logical:</strong></td> <td> <select name="server"> <option value="">select</option> <option value="server1">server1</option> <option value="server2">server2</option> <option value="server3">server3</option> <option value="server4">server4</option> </select> </td> </tr> <tr> <td colspan="2"> &nbsp; </td> </tr> <tr> <td> &nbsp; </td> <td> <input type="submit" name="action" value="Search" /> </td> </tr> </table> </form> EndOfText # get parameters my $q = CGI->new; my $action = $q->param('action'); my $server = $q->param('server'); # construct URL my $URL = join ";","http://a.com:8080/bms/healthMonitor.do?method=redi +rect", "operation=BackupHistorySearch", "subOperation=GetReport", "clientName=$server", "fromDateStr=$FM", "toDateStr=$TM"; # redirect if required if ($action eq '#Search'){ # remove hash to redirect print $q->redirect($URL); } else { print $q->header, $q->start_html('Page Title'), $page, # hash out next 2 lines or remove if OK $q->p("Vars action=$action : server=$server : FM=$FM : TM=$TM "), $q->a({-href=>$URL, -target=>'_blank'},$URL), $q->end_html; }
    poj

      Thanks.. However I need to input the server name(s) to search..

        In that case just replace the select
        <select name="server"> <option value="">select</option> <option value="server1">server1</option> <option value="server2">server2</option> <option value="server3">server3</option> <option value="server4">server4</option> </select>
        with a text box
        <textarea name="server" rows="1" cols="30" wrap="virtual"></textarea>
        poj

        Thanks for the help... Changed it to add text box. However this is opening in the same page. Is is possible to open on a new page and also check for mutiple servers

        #!/usr/bin/perl # n.cgi use strict; use CGI qw( :standard ); use CGI::Carp qw( fatalsToBrowser ); use POSIX qw(strftime); my $today = time; my $yesterday = $today - 60 * 168 * 60; my $FM = strftime "%m-%d-%Y", ( localtime($yesterday) ); my $TM = strftime '%m-%d-%Y', localtime; # page layout my $page =<< "EndOfText"; <h2>NBU Backup Report</h2> <form name="form" method="post" action="n.cgi"> <table> <tr> <td align="right"><strong>Hostname</strong></td> <TD> <TEXTAREA NAME="server" ROWS=1 COLS=30 WRAP="server"></TEXTAREA> </td> </tr> <tr> <td colspan="2"> &nbsp; </td> </tr> <tr> <td> &nbsp; </td> <td> <input type="submit" name="action" value="Search" /> </td> </tr> </table> </form> EndOfText # get parameters my $q = CGI->new; my $action = $q->param('action'); my $server = $q->param('server'); # construct URL my $URL = "http://a.com:8080/bms/healthMonitor.do?method=redirect&oper +ation=BackupHistorySearch&subOperation=GetReport&clientName=$server&f +romDateStr=$FM&toDateStr=$TM"; # redirect if required if ($action eq 'Search'){ # remove hash to redirect print $q->redirect($URL); } else { print $q->header, $q->start_html('Page Title'), $page, # hash out next 2 lines or remove if OK # $q->p("Vars action=$action : server=$server : FM=$FM : TM=$TM "), $q->a({-href=>$URL, -target=>'_blank'},$URL), }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-04-18 00:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (460 votes), past polls