in reply to Re: Embedded redirection
in thread Embedded redirection

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?

Replies are listed 'Best First'.
Re: Re: Re: Embedded redirection
by Fastolfe (Vicar) on Jan 13, 2001 at 05:29 UTC
    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;
      This code finally worked.
      my $result= new CGI; print $result->redirect("http://www.example.com"); close STDOUT; my $pid = fork; exit if $pid; die "fork: $!" unless defined $pid;
      However, is this the most efficient way of doing things? Is it going to clog the system up if the script is accessed very freqeuntly? Thanks.
        It won't have any measurable impact on your system. Remember, the total number of processes running remains the same. The parent exits just as it spawns the child. All you're changing is that the child continues working in the background while the parent exits gracefully, allowing the HTTP request to complete.