1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Redirecting URL in Perl

Discussion in 'Perl' started by pradeep, Dec 10, 2008.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    A very common technique used by many developers is to redirect the user to another URL or the referrer URL after doing some work e.g. processing a form. In Perl all headers have to printed before any actual content is outputed.

    A very simple way of redirecting to another URL
    Code:
    #!/usr/bin/perl
    
    print "Content-Type: text/html\n";
    print "Location: http://www.go4expert.com/no-page-here.html\n\n";
    
    
    Doing the same thing using the CGI module.
    Code:
    #!/usr/bin/perl
    
    use CGI;
    
    my $cgi = new CGI;
    
    print $cgi->header(-location => q[http://www.go4expert.com/no-page-here.html]);
    
    You may also pass the HTTP status code, sometimes it's needed e.g. if you pass a 302 status code, the URL of the page in the browser remains the same. Here's all the status codes used for redirecting a page.

    Code:
    301 Moved Permanently
    302 Found
    303 See Other
    
    So, here's how we redirect with the status.
    Code:
    #!/usr/bin/perl
    
    use CGI;
    
    my $cgi = new CGI;
    
    print $cgi->header(-location => q[http://www.go4expert.com/no-page-here.html], -status=>301);
    
     
    shabbir likes this.
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
  3. dsouza

    dsouza New Member

    Joined:
    Jan 10, 2009
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    nice post
     
  4. fazalca

    fazalca New Member

    Joined:
    Apr 16, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    print redirect('URL');

    will do the same thing
     

Share This Page