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?

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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...

2 Comments

But if you call anything like CGI->header() before the redirect, the redirect method does not work.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.