Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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


Replies are listed 'Best First'.
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):
    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 ( in the redirect
    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(''); } print $cgi->header(); # [...]
      Thanks a lot Monks

      That made the trick


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://695252]
Approved by kabeldag
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2017-07-24 06:57 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (348 votes). Check out past polls.