带有限制的 if 语句 php
我这样做是为了检查我的课程表,以便在没有课时提醒我。我将它设置为一个 cron 作业,每小时检查一次,但我的问题是,当没有课程时,我整天都会收到警报,并且无法停止它,除非我禁用 cron 作业。 我想知道如何才能将其限制为全天只有 5 个提醒?
$source = file_get_contents($website);
preg_match('#<classtime>(.*?)</classtime>#', $source, $match);
if (strlen($match[1])<32) {
mail('[email protected]', 'REMINDER!', 'No Class');
}
else
if (strlen($match[1])>32) {
mail('[email protected]', 'REMINDER!', 'No Class');
}
else {
echo "Check Class Schedule";
}
I made this to check my class schedule to remind me when there is no class. I set it on a cron job to check every hour, but my issue is that when there is no class I keep getting alerts all day and can't stop it unless I disable the cron job. What I would like to know is how I can limit this to only 5 alerts for the entire day?
$source = file_get_contents($website);
preg_match('#<classtime>(.*?)</classtime>#', $source, $match);
if (strlen($match[1])<32) {
mail('[email protected]', 'REMINDER!', 'No Class');
}
else
if (strlen($match[1])>32) {
mail('[email protected]', 'REMINDER!', 'No Class');
}
else {
echo "Check Class Schedule";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
保存文本文件(或使用数据库)来更新当天发送警报的次数,
如果大于 5,则不发送警报
save a text file (or use a database) to update how many times that day an alert was sent,
and if it is greater than 5 dont send the alert
将发送警报的计数存储在文件中的某个位置,每次检查时递增它,如果计数 > > 则不发送警报。 5. 午夜过后,将计数重置为 0。
Store the count of sent alerts in a file somewhere, increment it each time you check, and don't send alerts if the count's > 5. After midnight rolls around, reset the count to 0.
快速而简单的答案是创建一个文件,您可以读取和写入。例如。
如果你每次遇到“No class”场景时都使用这个,一旦达到5,如果读到5,就不再发送任何消息。然后为了简单起见,在午夜有一个 cron 作业将“0”回显到文件中。
Quick and simple answer is create a file, you read and write to. eg.
If you use this each time you encounter a "No class" senario, once it reaches 5, if the read says 5, dont send any more messages. Then to keep it simple, at midnight have a cron job to echo "0" into the file.
过去 24 小时内检查的电子邮件不超过 5 封。可以进行大规模改进,并且需要错误处理,但它应该可以工作:
Checks for no more than 5 emails in the last 24 hour period. Could be massively improved and it needs error handling, but it should work: