Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Perl web redirect

by racer_x (Sexton)
on Jun 10, 2002 at 15:09 UTC ( [id://173179]=perlquestion: print w/replies, xml ) Need Help??

racer_x has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks. I'd like to know a good way to redirect a web user to a certain URL from a perl script. For example, Java Servlets have a method response.sendRedirect("url") which redirects the user to that URL. What's the best way to do this kind of thing with Perl? I'd like to actually redirect the user and not just print out the contents of the target URL.


Replies are listed 'Best First'.
Re: Perl web redirect
by davis (Vicar) on Jun 10, 2002 at 15:15 UTC
    You could try:
    #!/usr/bin/perl use warnings; use strict; use CGI; my $q = new CGI; print $q->redirect(-uri=>"");
    For more information, see the docs on thusly:
    perldoc CGI
    Is this going out live?
    No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist
    Update: Fixed blockquote dumbness on my part
    Update 2: Changed "location" to "uri" as per little's catch below.
      Using CGI for this triviality seems like overkill.
      #!/usr/bin/perl print "Location:\n\n"; __END__
      Will do. Of course, using perl is overkill, you may as well write a one liner in C:
      int main () {printf ("Location\n\n");}


        For the simple case, agreed. Unfortunately it's difficult to tell from the poster's question whether or not this was the script's sole purpose.

        Please read the specs if you want to do it by hand and right. There is quite more to be remitted, eg. the HTTP Status code, which in this case would be "302", which means MOVED. No, I don't think that using would be overkill (what about tachyon's suggestion for a light weight module instead?), it just helps you to have easy acces to teh Interface without knowing each little detail. Additionallly it helps you to protect your server against attacks very easily.

        Another hint on CGI's redirection, the name for the parameter is "uri" not "location", eg.

        print redirect(-uri=>"", -nph=>1, -cookie=>$cookie)
        Please study what the output is to see what has to be remitted.

        Have a nice day
        All decision is left to your taste
Re: Perl web redirect
by tomhukins (Curate) on Jun 10, 2002 at 15:22 UTC
    If you had typed redirect into the search box that appears at the top of each page, you would have found an answer to your question right away. Before asking a question, it's a good idea to find out if the same question has already been answered.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://173179]
Approved by talexb
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-15 06:52 GMT
Find Nodes?
    Voting Booth?

    No recent polls found