Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Can't Get CGI Results Page to Redirect

by thezip (Vicar)
on Aug 03, 2009 at 20:30 UTC ( #785549=note: print w/ replies, xml ) Need Help??


in reply to Can't Get CGI Results Page to Redirect

socrtwo, it's difficult to follow your node because you have included too many extraneous details. I think you'll have much more specific success if you reduce the sheer amount detail in your node.

With that said, I'll offer up a couple of general comments which may or may not be of help, pending further explanations on your part:

  1. You only get one shot at printing a CGI header. Thus, if you've already printed out one header, it is impossible to get a redirect to work.
  2. Have you tried hardcoding a URL to see if that will redirect?

What can be asserted without proof can be dismissed without proof. - Christopher Hitchens


Comment on Re: Can't Get CGI Results Page to Redirect
Replies are listed 'Best First'.
Re^2: Can't Get CGI Results Page to Redirect
by socrtwo (Sexton) on Aug 03, 2009 at 22:45 UTC
    Thanks for your response. Sorry about the detail. I will go lighter I hope as I learn more Perl... Anyway, if you only get one shot at the header, should I write the redirect before the first line in the default header?
    print qq~Content-type: text/html\n\n~;?
    Yeah I hard coded the redirection and no redirecting occurred.
      Just don't print the header until you're ready to either print the page -or- generate a redirect.

      A technique you can use to defer printing the header is to build up your HTML output into a string, and then do:

      #!/usr/bin/perl use strict; use warnings; use CGI qw/:standard/; my $q = CGI->new(); my $do_redirect = 1; # for example only # ... build the entire output into a string $str = "This is your concatenated HTML doc"; if ($do_redirect) { print redirect($url); } else { print $q->header(), $str; }
      The idea is to wait until the absolute last second to print either header.

      What can be asserted without proof can be dismissed without proof. - Christopher Hitchens
        OK, I was able to find an open source uploader and intercept the header before it printed. Here' where I put the code:
        if ($action eq 'admin') { print $query->header; if ($login && $password) { &admin($query, $login, $password); } else { print &PagePassword($NAME_TITLE{'common_admin'}); } } elsif ($action eq 'upload') { ##### My code begins ##### use CGI qw(:cgi); use strict; `perl newimg2db.pl > newimgseekurl.txt`; sleep 1 while ( !(-e "newimgseekurl.txt") ); use File::Copy; copy("newimgseekurl.txt", "newimgseekurl2.txt") or die "File canno +t be copied."; my $file = "newimgseekurl.txt"; unlink($file); open FILE, "newimgseekurl2.txt" or die "Couldn't open file: $!"; my $newimgseekurl = <FILE>; close FILE; print redirect("$newimgseekurl"); ##### My Code Ends ##### if ($dir = &check_password('guest', $login, $password)) { print &Upload($query, $dir); } else { print &BadPassword($NAME_TITLE{'common_member'}); } } else { print $query->redirect($FORM_URL);

        This did not prevent the file from uploading and did properly redirect. Thanks for the help, views and voting.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2015-07-31 10:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (276 votes), past polls