We'd like to have PHP automatically delete a page from our website 24 hours after a specified date and time. The purpose is to offer promotional coupons to certain customers that last only 24 hours. How can we do this? (I'm ultra novice so please answer by example code rather than explanation. Thank you.)
我们想让PHP在指定日期和时间后24小时自动从我们的网站删除一个页面。其目的是为某些顾客提供24小时的促销优惠券。我们怎么做呢?(我是超级新手,所以请用示例代码来回答,而不是解释。谢谢你。)
What we have so far:
到目前为止我们所拥有的:
<?php
$filename = "myfile.txt";
sleep (86400);
unlink($filename);
?>
This doesn't make sense though because I think the page has to be open for a full 24 hours (86,400 seconds) for it to execute, yes? Also, the 24 hours countdown should start at a specified date and time. What code would do this?
这是没有意义的,因为我认为页面必须开放24小时(86,400秒)才能执行,是吗?此外,24小时倒计时应该从指定的日期和时间开始。什么代码会这样做?
Thank you so much!
谢谢你这么多!
2 个解决方案
#1
4
Don't delete it... just don't show it. I'd avoid using PHP via web to delete stuff on local system. Basically, wrap the code in an include where if with the date range show, otherwise don't.
不要删除它…只是不表现出来。我将避免使用PHP通过web删除本地系统上的内容。基本上,将代码包装在一个包含日期范围显示的地方,否则不要。
Better yet, store the offer details in a CSV file, XML doc, or database with start & finish date/times and use that for your check.
更好的方法是,将提供的细节存储在CSV文件、XML文档或数据库中,并使用开始和结束日期/时间,并将其用于您的检查。
#2
0
If you want things to happen at scheduled times as opposed to responses to user events, use they scheduling system built into the operating system. On Linux/Unix, that would be cron.
如果您希望事情在预定的时间发生,而不是响应用户事件,那么可以使用它们在操作系统中构建的调度系统。在Linux/Unix上,这是cron。
It is too detailed to explain here but it isn't terrible complicated. Just BE CAREFUL if you are deleting files.
这里解释得太详细了,但并不是很复杂。如果你正在删除文件,请小心。
Wikipedia has a good overview of cron here: http://en.wikipedia.org/wiki/Cron
*对cron有一个很好的概述:http://en.wikipedia.org/wiki/Cron。
Of course, using cron usually requires that you have terminal access to the server.
当然,使用cron通常需要对服务器进行终端访问。
#1
4
Don't delete it... just don't show it. I'd avoid using PHP via web to delete stuff on local system. Basically, wrap the code in an include where if with the date range show, otherwise don't.
不要删除它…只是不表现出来。我将避免使用PHP通过web删除本地系统上的内容。基本上,将代码包装在一个包含日期范围显示的地方,否则不要。
Better yet, store the offer details in a CSV file, XML doc, or database with start & finish date/times and use that for your check.
更好的方法是,将提供的细节存储在CSV文件、XML文档或数据库中,并使用开始和结束日期/时间,并将其用于您的检查。
#2
0
If you want things to happen at scheduled times as opposed to responses to user events, use they scheduling system built into the operating system. On Linux/Unix, that would be cron.
如果您希望事情在预定的时间发生,而不是响应用户事件,那么可以使用它们在操作系统中构建的调度系统。在Linux/Unix上,这是cron。
It is too detailed to explain here but it isn't terrible complicated. Just BE CAREFUL if you are deleting files.
这里解释得太详细了,但并不是很复杂。如果你正在删除文件,请小心。
Wikipedia has a good overview of cron here: http://en.wikipedia.org/wiki/Cron
*对cron有一个很好的概述:http://en.wikipedia.org/wiki/Cron。
Of course, using cron usually requires that you have terminal access to the server.
当然,使用cron通常需要对服务器进行终端访问。