带有限制的 if 语句 php

发布于 2024-11-04 09:14:01 字数 814 浏览 0 评论 0原文

我这样做是为了检查我的课程表,以便在没有课时提醒我。我将它设置为一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

祁梦 2024-11-11 09:14:01

保存文本文件(或使用数据库)来更新当天发送警报的次数,
如果大于 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.

蹲墙角沉默 2024-11-11 09:14:01

快速而简单的答案是创建一个文件,您可以读取和写入。例如。

while (( $fp = fopen ($counterfile,"r+")) == false)
{ usleep(5);}
       while (!flock($fp,2))
{ usleep(5); }
$data = fread($fp,filesize($counterfile));
if (rewind($fp)!=0)
{
        $data++;
        fwrite($fp,$data,strlen($data));
}
flock($fp,3);
fclose($fp);

如果你每次遇到“No class”场景时都使用这个,一旦达到5,如果读到5,就不再发送任何消息。然后为了简单起见,在午夜有一个 cron 作业将“0”回显到文件中。

Quick and simple answer is create a file, you read and write to. eg.

while (( $fp = fopen ($counterfile,"r+")) == false)
{ usleep(5);}
       while (!flock($fp,2))
{ usleep(5); }
$data = fread($fp,filesize($counterfile));
if (rewind($fp)!=0)
{
        $data++;
        fwrite($fp,$data,strlen($data));
}
flock($fp,3);
fclose($fp);

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.

杀お生予夺 2024-11-11 09:14:01

过去 24 小时内检查的电子邮件不超过 5 封。可以进行大规模改进,并且需要错误处理,但它应该可以工作:

$log_file = 'log.txt';
$log = file_get_contents( $log_file );
$timestamps = array_splice( explode( PHP_EOL, $log ), -5, 1 );
if ( count( $timestamps > 5 ) )
    {
    if ( end( $timestamps ) == '' )
        {
        array_pop( $timestamps );
        }
    $fifth_from_last_email = ($timestamps[0]);
    $time_24_hours_ago = time() - 3600 * 24;
    if ( $fifth_from_last_email < $time_24_hours_ago )
        {
        $fp = fopen( $log_file, 'a+' );
        fwrite( $fp, time() . PHP_EOL );
        fclose( $fp );
        mail( '[email protected]', 'REMINDER!', 'No Class' );
        }
    }

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:

$log_file = 'log.txt';
$log = file_get_contents( $log_file );
$timestamps = array_splice( explode( PHP_EOL, $log ), -5, 1 );
if ( count( $timestamps > 5 ) )
    {
    if ( end( $timestamps ) == '' )
        {
        array_pop( $timestamps );
        }
    $fifth_from_last_email = ($timestamps[0]);
    $time_24_hours_ago = time() - 3600 * 24;
    if ( $fifth_from_last_email < $time_24_hours_ago )
        {
        $fp = fopen( $log_file, 'a+' );
        fwrite( $fp, time() . PHP_EOL );
        fclose( $fp );
        mail( '[email protected]', 'REMINDER!', 'No Class' );
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文