tags:

views:

314

answers:

2

I have a Perl CGI script that creates a login screen, i.e. user name and password.

I want, after successful login, the user to be redirected to the next action within the application (another Perl CGI script).

What is the command to redirect one CGI script or to an HTML page?

+4  A: 

In HTTP terms you should have your program output a 302 or 303 status code and a Location header.

If you are using CGI.pm then you can use the redirect method to achieve this.

David Dorward
+3  A: 

An example using CGI as David Dorward mentions.

use CGI;

if (redirect_needed) {
    # If redirect is desired, don't print headers...
    print CGI->redirect("http://some.other.url/");
    exit; 
}

# If no redirect is desired...
print CGI->header();
# etc...
Adam Bellaire
But if you call anything like CGI->header() before the redirect, the redirect method does not work.
gath
@gath: Correct. Actions like sending redirects, setting cookies, and setting the `content-type` of your output are all actions that must occur in the HTTP headers, which in turn must be the first output from your script to STDOUT.
Adam Bellaire