我建立了一个Web应用程序,用户需要支付1/3/6个月的费用并加入该网站,我需要在用户帐户到期15天之前向该用户发送提醒邮件,我该如何实现?我不了解正确的逻辑...我正在将注册日期,到期日期存储在数据库中,这种下面的逻辑可以正常工作吗?
<?php
$expiringDate = "2015-07-21";
$todaydate = date("Y-m-d");
$date = date("Y-m-d",strtotime("-15 days", strtotime($expiringDate)));
if($todaydate == $date){
//send mail
}else{
//quit
}?>
而且如果今天即将到期,我也想更改数据库中的值...是在其他cronjob中执行还是更好,还是可以在上述代码中执行此操作?
<?php
$expiringDate = "2015-07-21";
$todaydate = date("Y-m-d");
$date = date("Y-m-d",strtotime("-15 days", strtotime($expiringDate)));
if($todaydate == $date){
//send mail
}else{
//check and change the value if today is the expiring
}?>
我走的路正确吗?这是否安全?是否还有其他更好的方法可以完成这项工作?
我建议每天执行cron工作。
然后,您的PHP脚本应检查15天后到期的所有人员。
但是,正如其他人指出的那样,如果您的Cron工作有一天失败了,您可能会错过一批人。
因此,我将检查剩余15天或少于15天的人员,这些人员在您的数据库中没有针对他们设置提醒标记。这意味着如果cron作业对于剩余15天的某人失败,那么在剩余的14/13/12等天,您的脚本将看到未针对他们设置提醒标志,并且仍将发送提醒。
<?php
$reminderSent = false; // Get this value from the db (true or false)
$expiryActioned = false; // Get this value from the db (true or false)
$expiringDate = strtotime('2015-07-21'); // Get this date from the db
$todayDate = time();
$reminderDate = strtotime("-15 days", $expiringDate);
if ($todayDate >= $reminderDate && $reminderSent == false) {
// Send mail
// Set flag $reminderSent in database to indicate reminder has been sent
} elseif ($todayDate >= $expiringDate && $expiryActioned == false) {
// Do something
// Set $expiryActioned in database to indicate the user has expired and something has been done about it
}
?>
但是,与其选择所有人并使用上述逻辑遍历所有逻辑,不如将上述逻辑构建到您的SQL查询中以提高效率。
快速示例:
// Select all users that expire in 15 days or less
SELECT `userid`, `name`, `email` FROM `user` WHERE NOW() >= DATE_SUB(`expiry`, INTERVAL 15 DAY) AND reminder_sent = 0
// Now loop through each user, send them an email and then:
UPDATE `user` SET reminder_sent = 1 WHERE `userid` = X
和
// Select all users that have expired
SELECT `userid`, `name`, `email` FROM `user` WHERE NOW() >= `expiry` AND `expiry_actioned` = 0
// Now loop through each user, do whatever you need to and then:
UPDATE `user` SET expiry_actioned = 1 WHERE `userid` = X
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句