如何在不使用cronjob的情况下运行php页面脚本 [英] How to run php page script without cronjob

查看:52
本文介绍了如何在不使用cronjob的情况下运行php页面脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想每5秒运行一次PHP脚本而不使用cronjobs.在PHP中怎么可能?

I want to run a PHP script every 5 seconds without using cronjobs. How is it possible in PHP?

我想每5秒更新一次用户数据.当我刷新页面时,该程序将执行,但是无论页面是否在浏览器中打开,我都希望运行该脚本.

I want to update user data every 5 seconds. The program will execute when I refresh the page but I want to run that script if the page is open or not in browser.

我该如何实现?

推荐答案

一种方法是拥有一个文本文件或一个数据库条目,其中包含上次运行时间(以UNIX时间为准).
然后在所有(或选定的)页面上添加如下内容:

One way to do it would be to have a text file or a database entry that holds the time of the last run in UNIX time.
Then on all (or selected) pages you add something like;

If($lastrun +5 < strtotime(now)){
    //Run the user update 
}

这意味着当您页面上的用户或访问者进入所选"页面之一时,该访问者上方的代码将运行更新"

This means when a user or visitor on your page goes to one of the "selected" pages with the code above this visitor will "run the update"

这篇关于如何在不使用cronjob的情况下运行php页面脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆