Alex Rivera | Logout

How do I close a client connection early, while continuing to run my code?

Asked 2008-09-26T09:02:00.780
115

I'm attempting to do an AJAX call that will initiate a fairly long process. I'd like PHP to simply send a response indicating that the process has started, but the JavaScript code won't return the response until PHP is done running.

I've tried this with a "close" header (below), and also with output buffering; neither seems to work. Any guesses? or is this something I need to do on the client side?

<?php
echo("We'll email you as soon as this is done.");
header("Connection: Close");
// do some stuff that will take a while
mail( 'dude@example.com', "okay I'm done", 'Yup, all done.' );
Edit
Report

1 Answer

1

You could try to do multithreading.

you could whip up a script that makes a system call ( using shell_exec ) that calls the php binary with the script to do your work as the parameter. But I don't think that is the most secure way. Maybe you can thighten stuff up by chrooting the php process and other stuff

Alternatively, there's a class at phpclasses that do that http://www.phpclasses.org/browse/package/3953.html. But I don't know the specifics of the implementation

answered 2008-09-26T09:10:51.163

Your Answer