dsc862009 2014-12-30 09:31
浏览 13
已采纳

如何设置新闻事件/公告在PHP中过期? [关闭]

PHP newbie here. My question is related to the logic/best practice for executing a certain task.

I have a news event/announcement being displayed on a website and needs to be removed/expire after a certain date/time. I have set a expiration date/time in the database, when a user visits the website after the date/time has passed a query is triggered and the news event/announcement is set to "0" (which is hidden).

I would like to know if this the best practice of accomplishing it or is there a better way?

Thanks.

  • 写回答

2条回答 默认 最新

  • dongpu42006096 2014-12-30 09:41
    关注

    The method you mention is usually effective, especially for small applications, but not a best practice. The reason it's not a best practice is because:

    Issues

    1. You are making the user wait for task execution
    2. If there is no activity on your website, these tasks will not be done
    3. If anything happens while executing a task, your user will receive the errors

    The first two might not seem to matter much, but that's only because the tasks you have now are very fast and non-critical. However, if a task would take a second or two, that would mean the user now has to wait 2 extra seconds before he sees the page, which is bad.

    Likewise, if nobody visits your site for a week and there's a list of 15 tasks that need to be done, the user now would have to wait for 30 seconds. It might even time out the whole page; which would mean your tasks are now unfinished and the user is annoyed with getting a timeout for seemingly no reason.

    In addition, if one of your tasks is time critical, it still won't be done. For example if the task is to send someone a reminder email after 24 hours but nobody logs in, the mail won't be sent.

    The last one is also a problem; both because this makes it hard to see when a task fails (as the error is logged as a user problem, if at all) and because your user (again for no reason) is now looking at an error screen.

    Solution

    If you want to use the best practice, move all these sorts of tasks to either a Scheduled Task (under windows) or a Cronjob (under unix). This means you have a system service that periodically starts up and executes a PHP script that can do maintenance to your site, such as removing these news messages, sending out emails, or other things.

    This has a number of advantages:

    1. The server will always be there on time to run the tasks when they need to be run
    2. You can disable timeouts and upgrade memory availability to run intensive tasks
    3. Users will not have to wait for anything to complete
    4. You can add special logging to the server, so that you know when these important but hidden tasks fail

    Most providers allow you to set these kinds of tasks even on cheap hosting packages.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型