Reputation: 617
I want to send an AJAX call to a server that makes phone calls to our clients. However, I don't want the person who starts the process to have to leave their browser open. After some research, I've found that I can pass a header in php normally to allow the browser to disconnect without the php execution stopping. Will this work for AJAX as well? The relevent code is below.
//jQuery 1.8.10
$('#start').click(function(){
$.post('/startcalling', {
'time_limit': 0
});
});
$('#stop').click(function(){
$.post('/stopcalling');
});
The important PHP
function startCalling(){
echo "starting...";
header('Connection: close');
while(getFlagStatus()){
makeNextCall();
}
}
function stopCalling(){
if(!getFlagStatus()){
$query = "UPDATE cc_flags SET cc_on = 0";
mysql_query($query);
}
}
I could package this into a CLI command, and accomplish my goal, but I feel like there has to be an easier way. If I've been confusing, let me know and I'll edit.
EDIT: Does php execution stop after a user leaves the page? Does this apply here?
Upvotes: 0
Views: 306
Reputation: 338
I looks like you want the behavior of ignore_user_abort($bool). But this alone won't help you much as PHP usually have a time limit of 30 seconds. set_time_limit($seconds) probably will help you with this.
Keep in mind that the script will run on your webserver and the user will not have any way to kill the script if they make a mistake, so don't forget to add extra checks so your users don't spawn the script 10 times and let it run, especially if it's making phone calls!
I'd suggest making a server script that runs outside of the web server, so you can communicate with the daemon and start/stop jobs on it. It's faily easy to do with simple sockets.
Upvotes: 1