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

I have seen in PHP a way to redirect the user to another site and was wondering if there was a way to do this in PERL?

Here is the code I saw for PHP
<script language="php"> function redirect($location) { /* routine to redirect the client to another location (safe for al +l browsers) */ header("Location: $location"); echo "<HTML><HEAD><META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0;URL=$lo +cation\"></HEAD><BODY><A HREF=\"$location\">Click Here</A></BODY></HT +ML>"; exit; }

Replies are listed 'Best First'.
(chromatic) Re: Redirection on a Website
by chromatic (Archbishop) on Jul 20, 2000 at 20:40 UTC
    Use CGI.pm:
    use CGI; my $q = CGI::new(); print $q->redirect('http://perlmonks.org');
Re: Redirection on a Website
by cwest (Friar) on Jul 20, 2000 at 20:49 UTC
    Isn't this in the faq?
    shell> perldoc -q Redirect Found in /path/to/pod/perlfaq9.pod How do I Redirect to another page? Instead of sending back a `Content-Type' as the headers of your reply, send back a `Location:' header. Officially this should be a `URI:' header, so the CGI.pm module (available from CPAN) sends back both: Location: http://www.domain.com/newpage URI: http://www.domain.com/newpage Note that relative URLs in these headers can cause strange effects because of "optimizations" that servers do. $url = "http://www.perl.com/CPAN/"; print "Location: $url\n\n"; exit; To target a particular frame in a frameset, include the "Window-target:" in the header. print <<EOF; Location: http://www.domain.com/newpage Window-target: <FrameName> EOF To be correct to the spec, each of those virtual newlines should really be physical `"\015\012"' sequences by the time you hit the client browser. Except for NPH scripts, though, that local newline should get translated by your server into standard form, so you shouldn't have a problem here, even if you are stuck on MacOS. Everybody else probably won't even notice. shell>
    I feel so much better knowing that it's documented in the standard distro at perlfaq9.

    And thank goodness that it's available online, or most likeley, right on the box perl is on!

    Enjoy

    --
    Casey
    
Re: Redirection on a Website
by le (Friar) on Jul 20, 2000 at 21:51 UTC
    If you're running under mod_perl, you could do a (internal) redirect like that (from perldoc Apache):
    my $r = shift; # Get the Apache request $r->internal_redirect( $newplace ) Redirect to a location in the server namespace without telling the client. For instance: $r->internal_redirect("/home/sweet/home.html");