How to run php page script without cronjob

不羁岁月 提交于 2021-02-05 09:54:13

问题


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

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.

How can I achieve this ?


回答1:


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"




回答2:


You must have some basics about PHP. PHP only runs when you request a page. So it's impossible to run php without requesting the page. Somehow you must reload the page every 5 seconds then you can run it every 5 sec.

So you must use cron or something like this. Or, you can you can use an old computer (NOT RECOMMENDED) which will relaod the page. To relaod the page you can use browser plugins like auto reload (for chrome).

But almost every hosting companies provide free cron job. Please search your cpanel for that. Or, mail your hosting provider for help. Cron is the best way to do this job for free.



来源:https://stackoverflow.com/questions/45027658/how-to-run-php-page-script-without-cronjob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!