NYCPHP Meetup

NYPHP.org

[nycphp-talk] send header before end of script?

Hans Zaunere hans at nyphp.org
Thu Oct 23 19:21:29 EDT 2003



Stephen Musgrave wrote:

> I have a bulk email script that sends emails to about 1000 recipients.  The
> script has sleeps in it that cause it to run for about 2 hours after the
> user initiates it.  Due to the nature of request/response, the user will not
> get a confirmation message until 2 hours later.  I have set
> ignore_user_abort and I would like to send the a confirmation screen to the
> user that tells them that, "the process has been started and it will be
> completed in 2 hours."  But from what I can tell, sending the header
> redirect to the confirmation page aborts the execution of the script.
> Sounds like a chicken before the egg thing.  Any solutions?

I'm assuming you're using PHP as an Apache module, in which case you need to fork so that the mailing script is independant of Apache.  Otherwise, they share file descriptors and you'll run into problems.

I'm going to be finalizing a php component for this type of thing, but in general it might go something like:

[ in a script that is used in apache ]

	   $saddresses = serialize($addresses);
	   $mpp = popen('/usr/local/bin/mailer.php > /dev/null 2>&1','w');
	   fwrite($mpp,$saddresses);
	   pclose($mpp);


[ in a command line script ]

$saddresses = file_get_contents('/dev/stdin');

$pid = pcntl_fork();

if( $pid )
   exit(1);

posix_setsid();

$pid = pcntl_fork();

if( $pid )
   exit(1);

$addresses = unserialize($saddresses);


H






More information about the talk mailing list