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

I have a script that takes a users login, verfies, performs some tasks (like cleanup..etc) and then redirects the user to an inner page. Basically, the script checks the login information and if it is correct it prints out
print "Content-type: text/html\n\n"; print "<META HTTP-EQUIV=\"REFRESH\" content=\"0; URL=url">".

The script then performs a couple of other misc. tasks like directory cleanup. However, the cleanup tasks take about 10 seconds to complete and the user is not redirected to the page until the script has finished executing even though I put the Meta refresh line directly after login verification (before cleanup starts). I don't want the user to have to wait for the directory cleanup to be complete before being redirected. Is there anyway to redirect the user and have the script finish running in the background?
Thanks,
Tom

Replies are listed 'Best First'.
Re: Embedded redirection
by Fastolfe (Vicar) on Jan 13, 2001 at 05:08 UTC
    You probably want to take advantage of a standard HTTP redirect. Using <meta> tags in this fashion is highly questionable. Fortunately, CGI.pm makes this easy.
    use CGI ':standard'; # do processing print redirect('http://example.com/');
    At this point you may just have to close STDOUT for the server to notice the response is concluded. Otherwise, you can fork your processing into the background and have the parent exit normally.
    my $pid = fork; exit if $pid; die "fork: $!" unless defined $pid; # or maybe just warn # continue processing
      Using CGI.pm to do redirection works fine. However, when I close STDOUT, the user is immediately redirected but the rest of my script no longer executes defeating the purpose of trying to redirect early. Is forking the only way to make this work?
        I don't know; I'd have to experiment. The server may be killing you. Someone else may have better information, but I would try ignoring some signals. I suspect the server would send a HUP in a case like this, but it might be more aggressive.
        $SIG{HUP} = $SIG{INT} = $SIG{TERM} = 'IGNORE';
        Since that makes it a bit harder to kill a run-away process, you might want to set a timeout so your script commits suicide after a reasonable amount of time.
        $SIG{ALRM} = sub { die "$$ timed out" }; alarm 30;
Re: Embedded redirection
by merlyn (Sage) on Jan 13, 2001 at 10:39 UTC