Problem
I have a form that, when submitted, will run basic code to process the information submitted and insert it into a database for display on a notification website. In addition, I have a list of people who have signed up to receive these notifications via email and SMS message. This list is trivial as the moment (only pushing about 150), however it's enough to cause it takes upwards of a minute to cycle through the entire table of subscribers and send out 150+ emails. (The emails are being sent individually as requested by the system administrators of our email server because of mass email policies.)

During this time, the individual who posted the alert will sit on the last page of the form for almost a minute without any positive reinforcement that their notification is being posted. This leads to other potential problems, all that have possible solutions that I feel are less than ideal.

  1. First, the poster might think the server is lagging and click the 'Submit' button again, causing the script to start over or run twice. I could solve this by using JavaScript to disable the button and replace the text to say something like 'Processing...', however this is less than ideal because the user will still be stuck on the page for the length of the script execution. (Also, if JavaScript is disabled, this problem still exists.)

  2. Second, the poster might close the tab or the browser prematurely after submitting the form. The script will keeping running on the server until it tries to write back to the browser, however if the user then browses to any page within our domain (while the script is still running), the browser hangs loading the page until the script has ended. (This only happens when a tab or window of the browser is closed and not the entire browser application.) Still, this is less than ideal.

(Possible) Solution
I've decided I want to break out the "email" part of the script into a separate file I can call after the notification has been posted. I originally thought of putting this on the confirmation page after the notification has been successfully posted. However, the user will not know this script is running and any anomalies will not be apparent to them; This script cannot fail.

But, what if I can run this script as a background process? So, my question is this: How can I execute a PHP script to trigger as a background service and run completely independent of what the user has done at the form level?

EDIT: This cannot be cron'ed. It must run the instant the form is submitted. These are high-priority notifications. In addition, the system administrators running our servers disallow crons from running any more frequently than 5 minutes.

推荐答案

Doing some experimentation with exec and shell_exec I have uncovered a solution that worked perfectly! I choose to use shell_exec so I can log every notification process that happens (or doesn't). (shell_exec returns as a string and this was easier than using exec, assigning the output to a variable and then opening a file to write to.)

I'm using the following line to invoke the email script:

shell_exec("/path/to/php /path/to/send_notifications.php '".$post_id."' 'alert' >> /path/to/alert_log/paging.log &");

It is important to notice the & at the end of the command (as pointed out by @netcoder). This UNIX command runs a process in the background.

The extra variables surrounded in single quotes after the path to the script are set as $_SERVER['argv'] variables that I can call within my script.

The email script then outputs to my log file using the >> and will output something like this:

[2011-01-07 11:01:26] Alert Notifications Sent for http://alerts.illinoisstate.edu/2049 (SCRIPT: 38.71 seconds)
[2011-01-07 11:01:34] CRITICAL ERROR: Alert Notifications NOT sent for http://alerts.illinoisstate.edu/2049 (SCRIPT: 23.12 seconds)

Php相关问答推荐

在不刷新页面的情况下无法使用JQuery更新id

SQLSTATE [42S02]:找不到基表或视图:1146 Table eshop. sessions不存在'''

在特定日期之间使用特定的元密钥销售产品

与会者在WooCommerce中按购物车项目数量自定义 checkout 字段

如何修复条带元素中的错误&&客户端_机密&

Laravel Livewire如何在数组值上使用IF、ELSE条件

EBay Inventory API createOrReplaceInventoryItem出错

根据WooCommerce中的客户计费国家/地区更改产品价格

如果为布尔值,则 for each 循环重新启动

按列值将二维数组排序为不超过N个的递增组

在WooCommerce上克隆订单时使用`add_product()`添加产品元数据

为什么PHP PDO忽略NOT NULL约束?

更改WooCommerce checkout 中的错误消息以获得不可用的送货方式

使用 phpseclib 验证 RSA-PSS

防止使用woocommerce_checkout_process下单

Shopware 6.4.20店铺激活问题

PHP header() 是否缓存重定向,如果是,如何防止它这样做?

Laravel Websockets 错误:(WebSocket 在建立连接之前关闭)

如何在 Laravel 中验证多个输入?

如何在不复制的情况下在php中重新索引数组