我在 PHP 中有一个警报脚本,它用某种紧急文本向应用程序发出警报。现在我需要以某种方式添加一些代码,允许管理员将警报设置为定时警报。目前他们可以设置警报,然后发送另一个请求来删除警报。
下面的代码已被更改以反映我当前使用的内容,我大部分都在工作,除了时间到期后的删除,也就是 goto a; 常规。
$filename = "alertCheck.php";
$timedalertsingle = $_POST['timedalertsingle'];
$timedalerthours = $_POST['timedalertchoice'];
$timestamp = time();
$timedalertseconds = $timedalerthours * 3600;
// Testing the variables
echo $timedalertsingle."<br />";
echo $timedalerthours."<br />";
echo $timestamp."<br />";
echo $timedalertseconds."<br />";
echo ($timestamp + $timedalertseconds)."<br />";
// write the file
$fp = fopen ($filename, "w");
if ($fp) {
fwrite ($fp, $timedalertsingle);
fclose ($fp);
echo ("<span style='color: #6FF;'>File written:</span> " . " This alert will last for " .$timedalerthours . " Hour(s)" . "<br />");
echo date('m-d-y') ."<br />";
}
else {
echo ("File was not written");
}
// test the time
a:
if (time() > ($timestamp + $timedalertseconds)){
// overwrite the file
fwrite ($fp);
fclose ($fp);
echo ("<span style='color: #6FF;'>There are no alerts.</span> ");
}
else {
goto a;
}
我的输入(更改以反映新的变量名称:
<form action="processtime.php" method="post" name="timedalert">
<strong>Post a Timed Alert: </strong><input name="timedalertsingle" size="60" type="text" /><br />
<strong>How many hours this alert will be displayed: </strong>
<select name="timedalertchoice" />
<option value=".0833">5 Minutes</option>
<option selected="selected" value="1">1 hour</option>
<option value="2">2 hours</option>
<option value="4">4 hours</option>
<option value="6">6 hours</option>
<option value="8">8 hours</option>
<option value="12">12 hours</option>
<option value="18">18 hours</option>
<option value="24">24 hours</option>
</select>
<input name="submit" type="submit" value="submit" /><br />
</form>
所以这是我的 processtime.php 页面运行后的样子:
树木倒下 93 号楼的一半在 Cuppertino
24
1354217471
86400
1354303871
文件写入:此警报将持续 24 小时
11-29-12
问题是它不会在给定时间后停止警报。帮助!TIA