Imagine a call center that cannot be inundated with customers by more than 2 calls per minute. So, anyone outside that range, would get an email support link ($bTrigger = FALSE). Everyone else ($bTrigger = TRUE) would get a tech support phone number.
The script is PHP. So, what’s the most efficient and accurate way to build this?
This is what I have so far, but unfortunately it’s only triggering once per minute. I can’t seem to figure out why it won’t run twice per minute.
<?php
$bTrigger = FALSE;
$sDir = dirname(__FILE__);
$sDir = rtrim($sDir,'/');
$sFile = $sDir . '/MINUTE-TIMER.txt';
$sLine = @ file_get_contents($sFile);
$sLine = str_replace("\r\n",'',$sLine);
$sLine = str_replace("\r",'',$sLine);
$sLine = str_replace("\n",'',$sLine);
$sLine = str_replace("\t",'',$sLine);
$asParts = explode(',',$sLine);
$nLetThru = @ $asParts[0];
$nLetThru = intval($nLetThru);
$nLastMin = @ $asParts[1];
$nLastMin = intval($nLastMin);
$nCurMin = intval(date('i'));
if (empty($sLine)) {
$nLetThru = 0;
$nLastMin = 0;
}
$nMaxLetThru = 2;
if ($nCurMin != $nLastMin) { // meaning, a new minute since last checked
if ($nLetThru <= $nMaxLetThru) { // meaning, we haven't hit more than max allowed
$bTrigger = TRUE;
++$nLetThru;
file_put_contents($sFile,"$nLetThru,$nCurMin");
} else {
file_put_contents($sFile,"0,$nCurMin");
}
}
if ($bTrigger) {
echo 'TRIGGERED!!!!';
} else {
echo 'not triggered';
}
The problem was a simple coding error: $nLetThru wasn’t being reset when the minute changed. (Also, your <= should have been <, but you already noticed that.)
Here’s the fixed code (based on the original version, in the question):
…