在PHP中延迟10分钟后执行代码 [英] Execute code after a 10 minute delay in PHP

查看:323
本文介绍了在PHP中延迟10分钟后执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在事件(提交表单)后的10分钟内延迟执行PHP中的某些代码(例如,发送电子邮件)。

I need to delay the execution of some code in PHP (for example; sending an email) by 10 minutes after an event (form submission).

什么是最好的方法?

我唯一的选择是每分钟运行一次Cronjob吗?

Would be my only option be to run a Cronjob every minute? Is this practical on shared hosting?

推荐答案

使用cronjobs是最好的方法。

Using cronjobs is the best way.

如果您不能在共享主机上使用cronjob(请咨询客户支持),则可以在连接到互联网(即您的家)的计算机上运行cronjob计算机),该服务器将wget运行到服务器上的php页面,在其上进行身份验证,然后运行php代码以发送电子邮件。

If you can't use a cronjob on your shared hosting (ask the customer support), you can run a cronjob on a machine connected to the internet (i.e. your home computer) that runs a wget to a php page on your server, authenticate on it and then run the php code to send your email.

对于PHP代码部分,我是ll将数据库表与所有要发送的电子邮件一起使用,一个creation_date字段和一个status字段。

For the PHP code part I'll use a database table with all the emails to be sent, a creation_date field and a status field.

作业调用的PHP代码很简单(用伪代码) ):

Your PHP code called by the job will simply do (in pseudo code):

$batchRecords = takeAbunchOfRecordsWhereStatus(NOT_SENT);
while($batchRecords) {
    if($creationDate + 10 minutes >= now()) {
        sendEmail();
        markRecordAsSent();
    }
}

这篇关于在PHP中延迟10分钟后执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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