我怎样才能在后台运行PHP脚本表单提交后? [英] How can I run a PHP script in the background after a form is submitted?

查看:118
本文介绍了我怎样才能在后台运行PHP脚本表单提交后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题结果
我有,提交时,将运行基本code来处理提交的信息,并将其插入到用于在通知网站显示的数据库的形式。此外,我对谁已经签署了接收通过电子邮件和短信通知这些人的名单。这份名单是微不足道的瞬间(仅推约150),但它足以引起它需要向上一分钟循环通过用户的整个表并发送150+电子邮件。 (该电子邮件是由因为群发电子邮件的政策我们的邮件服务器的系统管理员的请求被单独发送。)

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. 首先,海报可能会认为服务器滞后,再次点击提交按钮,导致脚本在启动或运行两次。我可以使用JavaScript来禁用按钮和替换的文本说像解决这个处理...,然而,这是不太理想的,因为用户仍然会在网页卡住脚本执行的长度。 (另外,如果JavaScript被禁用,这一问题依然存在。)

  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.)

二,海报可能会提交表单后pmaturely关闭的选项卡或浏览器$ P $。该脚本将保留在服务器上运行,直到它试图写回浏览器,但是如果用户随后浏览任何网页我们的领域内(而脚本仍在运行),浏览器挂起加载页面,直到脚本已经结束。 (当浏览器选项卡或窗口被关闭,而不是整个浏览器应用程序这只是发生。)不过,这是不太理想的。

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.

但是,如果我可以运行什么该脚本作为后台进程?所以,我的问题是:我怎么能执行一个PHP脚本触发作为后台服务并运行完全独立于什么样的用户在表单级别完成

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?

编辑:此的无法的被cron'ed。它必须运行在表单提交的瞬间。这些是高优先级的通知。此外,在运行我们的服务器的系统管理员禁止crons运行任何更频繁地超过5分钟。

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.

推荐答案

做一些实验用 EXEC 了shell_exec 我已经发现了非常完美的解决方案!我选择使用了shell_exec 这样我就可以记录每一个通知过程出现这种情况(或没有)。 (了shell_exec 返回一个字符串,这是比使用更方便 EXEC ,分配输出到一个变量,然后打开文件写入。)

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 &");

要注意&功放是很重要的; 在命令的末尾(由净@ codeR指出)。这个UNIX命令在后台运行的进程。

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.

路径的脚本后,包围在单引号额外的变量设置为 $ _ SERVER ['argv']作为变量,我可以我的脚本中调用。

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.

电子邮件脚本中使用&GT然后输出到我的日志文件键,将输出是这样的:

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脚本表单提交后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆