Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

problem with redirecting to a cgi page

by srinivas_rocks (Sexton)
on Jul 03, 2008 at 00:34 UTC ( #695252=perlquestion: print w/ replies, xml ) Need Help??
srinivas_rocks has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Seek your help on redirecting a page in CGI I have written a simple CGI page which accepts the username and password. From obtained values I pass them to a subroutine which creates cookies and returns to main. In main routine I redirect it to another page. Here is my sample code:

#! /usr/bin/perl use CGI; print $cgi->header(); print $cgi->start_html(-title=>'Login',-script=>$JSCRIPT); print qq{<center><link rel=stylesheet href="/wadmin.css" type="text/cs +s">}; if( $cgi->param('smbt')) { &creat_cooke(); print $cgi->redirect("Welcome.cgi"); } else { print qq{<form name="frm" method="POST">}; print qq{User<input type ="text" name="login" size=14 maxlength=10>}; print qq{Password <input type ="password" name="password" size=14 maxl +ength=10>}; print qq{<input type="submit" name="smbt" value="Submit" onclick="retu +rn verify()">}; }
When the code reaches to redirecting part it is giving me Status: 302 Moved Location: sample.cgi

Did I make any mistake while redirecting or any other part. Is that related to cookies ( as per my knowledge it should not )

Please help me with this.

Thanks in Advance

Srinivas.

Comment on problem with redirecting to a cgi page
Download Code
Re: problem with redirecting to a cgi page
by grep (Monsignor) on Jul 03, 2008 at 01:09 UTC
    Straight from the CGI Docs (emphasis from doco):
    GENERATING A REDIRECTION HEADER
    print redirect('http://somewhere.else/in/movie/land');

    Sometimes you don't want to produce a document yourself, but simply redirect the browser elsewhere, perhaps choosing a URL based on the time of day or the identity of the user.

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

    You should always use full URLs (including the http: or ftp: part) in redirection requests. Relative URLs will not work correctly.

    So you have 2 problems:

    • You have to do your check before you print any header and then print either the redirect or the standard header
    • You should use the full URL (http://foo.com/Welcome.cgi) in the redirect
    grep
    One dead unjugged rabbit fish later...
Re: problem with redirecting to a cgi page
by CardinalNumber (Prior) on Jul 03, 2008 at 01:14 UTC
    [update]As grep said,[/update] you're sending a full set of headers (and even content) before your code gets to send the redirect header. You need to have things in this order:
    #!/usr/bin/perl use CGI; my $cgi = new CGI; if ($cgi->param('smbt')) { &creat_cooke(); # sic exit print $cgi->redirect('http://example.com/path/Welcome.cgi'); } print $cgi->header(); # [...]
      Thanks a lot Monks

      That made the trick

      Srinivas.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://695252]
Approved by kabeldag
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-12-19 10:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (78 votes), past polls