I want to run a php script that will send out a bunch of emails (newsletters) but since it can take a while for that to run I want the user be immediately redirected to a page with a message like “Your newsletters are queued to be sent out.”
So far the header redirects work but they do not cause the redirection until after all the emails get sent. Can I close the connection after sending the redirect so the browser acts on the redirect immediately? I tried stuff like this
ob_start();
ignore_user_abort(true);
header( "refresh:1;url=waitforit.php?msg=Newsletters queued up, will send soon.");
header("Connection: close");
header("Content-Length: " . mb_strlen($resp));
echo $resp;
//@ob_end_clean();
ob_end_flush();
flush();
in various permutations and combinations but to no avail.
I suspect it cannot be done so simply but before I start messing with cron jobs or custom daemons I thought I’d look into this approach.
Thanks
eg like blockhead suggests below, but no luck
ob_start();
ignore_user_abort(true);
header( "refresh:1;url=mailing_done.php?msg=Newsletters queued for sending.");
header("Connection: close");
header("Content-Length: 0" );
//echo $resp;
ob_end_flush();
flush();
If you want to run script continuously and still want to display some output then, why don’t you use an ajax request to server which can queue mails and still let user continue browsing that page.
If you don’t want to use this; instead you could use,
the script runs background even if the user will be redirected to
show_usermessage.phppage