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

Redirection to one cgi page from another is not working

by nadarajan_v (Initiate)
on Nov 11, 2013 at 17:18 UTC ( #1062026=perlquestion: print w/ replies, xml ) Need Help??
nadarajan_v has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am sending data from A.cgi page to B.cgi. The later page updates the data into database and supposed to redirect back to A.cgi and the A.cgi page should display the updated data. Aftere going through PerlMonk's archived answers, I wrote the following lines in the B.cgi page, right after the database update code:

$url = "http://Myserver/Travel/cgi-bin/A.cgi/"; print "Location: $url\n\n"; exit();

After successfully finishing the database jobs, the page simply prints Location:http://Myserver/Travel/cgi-bin/A.cgi/ and stays at B.cgi and never gets redirectd to A.cgi.

Any help to resolve this issue is greatly appreciated.

Thanks

Comment on Redirection to one cgi page from another is not working
Select or Download Code
Re: Redirection to one cgi page from another is not working
by hippo (Deacon) on Nov 11, 2013 at 17:40 UTC

    Have a look at the redirect method in CGI - it should do what you want.

Re: Redirection to one cgi page from another is not working
by tangent (Deacon) on Nov 11, 2013 at 18:08 UTC
    the page simply prints Location:http://Myserver/Travel/cgi-bin/A.cgi/
    It looks like you have another print statement somewhere before the print "Location:..." one. Get rid of that and it should work.
Re: Redirection to one cgi page from another is not working
by Your Mother (Canon) on Nov 11, 2013 at 18:16 UTC

    tangent beat me to it but I already wrote it and it covers some CGI stuffage, so here it is.

    Probably you have something like this–

    use strict; use CGI ":standard"; # Lots of logic that somewhere results in printing a header... print header(); # And THEN you try to redirect.... my $url = "http://Myserver/Travel/cgi-bin/A.cgi/"; print "Location: $url\n\n"; exit;

    Which results in–

    Content-Type: text/html; charset=ISO-8859-1 Location: http://Myserver/Travel/cgi-bin/A.cgi/

    You only get to print headers once. It’s also possible, but unlikely, your server is configured to send headers for you. As hippo mentioned, the redirect is probably better written this way–

    print redirect( -location => $uri );
Re: Redirection to one cgi page from another is not working
by taint (Chaplain) on Nov 11, 2013 at 22:19 UTC
    Greetings, nadarajan_v.

    I'm not sure what your overall use of this is. But it might be worth looking at the pertinent RFC's HTTP status code(s). As I can never remember them, I took the liberty of creating a quick "cheat sheet". If your interested, here's the link to the pertinent section. I only mention it, because it is relevant to pass the correct status. ie;

    #!/usr/bin/perl -w use strict; print "Status: 301 Moved Permanently\n"; print "Location: http://COMPLETE/URI\n\n"; # complete URI (not relative) is important for proxies/squid/and HTTP- +1.0 # Sending STATUS _first_ also matters exit;
    You can also deliver CONTENT-TYPE on a conditional basis
    #!/usr/bin/perl -Tw #accepts application/xhtml? if ($ENV{'HTTP_ACCEPT'} =~ /application\/xhtml\+xml/) { print "content-type:application/xhtml+xml; charset=utf-8\n\n"; } #no? then text/html they get else { print "content-type:text/html; charset=utf-8\n\n"; }
    Wouldn't it be fun to boast of being XHTML compliant, even when your not?
    if ($ENV{'HTTP_USER_AGENT'} =~ /W3C\_CSS\_Validator\_JFouffa/) { print "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +; print "<?xml-stylesheet href=\"w3c.css\" type=\"text\/css\" media= +\"screen, projection, print\" ?>\n"; print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML 2.0// +EN\" \"http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n"; print "<head>\n"; my $server = $ENV{SERVER_NAME}; print " <title>"; print $server; print "</title>\n"; print " <meta http-equiv=\"content-type\" content=\"application\/xh +tml+xml; charset=utf-8\" />\n"; } else { print "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +; print "<?xml-stylesheet href=\"css3.css\" type=\"text\/css\" media +=\"screen, projection, print\" ?>\n"; print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML 2.0// +EN\" \"http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n"; print "<head>\n"; my $server = $ENV{SERVER_NAME}; print " <title>"; print $server; print "</title>\n"; print " <meta http-equiv=\"content-type\" content=\"application\/xh +tml+xml; charset=utf-8\" />\n"; print " <link rel=\"stylesheet\" type=\"text/css\" href=\"css3.css\ +" media=\"screen, projection, print\" />\n"; }
    Note the different stylesheet delivered, based on UA (User Agent).

    FWIW INMHO CGI (cgi.pm) is evil, and should be avoided at all cost. The simplicity of it's use, and making your code appear clean, and tidy. Make it's usage very tempting. But it is my experience, that it abstracts one so far away from what they're doing, that over time, one can easily create code that encounters issues. Making it an unnecessary job to diagnose, and correct. In short; if you don't have to think about something long enough. You'll forget it -- hope that made sense to more than just me. :)

    Best wishes.

    Chris

    #!/usr/bin/perl -Tw
    use Perl::Always or die;
    my $perl_version = (5.12.5);
    print $perl_version;
Re: Redirection to one cgi page from another is not working
by Anonymous Monk on Nov 11, 2013 at 23:31 UTC

      #/usr/bin/perl -wT

      use CGI and die;



      Sorry. Somewhat off topic. But couldn't resist.

      --Chris

      #!/usr/bin/perl -Tw
      use Perl::Always or die;
      my $perl_version = (5.12.5);
      print $perl_version;

        Sorry. Somewhat off topic. But couldn't resist.

        Yes, its hard to resist saying stupid things

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2014-07-25 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls