简体   繁体   中英

How to run php page script without cronjob

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 ?

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM