简体   繁体   中英

PHP game update

Say I have a game in PHP which is a money making game and everyday the prices on items change on the market.

How would I make it so the prices in the database are automatically updated each day? Would I install a program on the server that detects when it has gone 24 hours then runs a PHP file which does the updating? Or os there another way?

Edit: The thing is guys, I don't actually own the server I rent it from a hosting company so I don't really have access the the command line :s

Thanks, Stanni

Assuming you're on a Unix system, you should setup a daily cronjob. To do this, run "crontab -e" and enter something like:

9 21 * * * /path/to/your/script

This will run at 21:09 every day.

If you don't have access to the commandline, you could add a 1x1 image to the website which calls a php script which checks if there needs something be updated.

something like

<img style="width: 1px; height: 1px; visibility: hidden" src="cron.php">

In cron.php you check if the data needs to be updated.

Since you probably don't have access to cron either what I would do is check how much time has passed everytime someone loads a page. If 24 hours have passed then call your update function. If 48 hours have passed then call it twice. If no one loads the page then it doesn't matter if the update function has been called or not because no one is looking ;)

Or you could setup a computer at home to call your update.php remotely every 24 hours. You can do that with a cron job and wget or if you're using windows you could use the task scheduler.

I think the first option will work the best. Call your update function every page load and only update when the 24 hour mark has passed. If you write it correctly it doesn't matter if it gets updated at the exact 24 hour mark.

You want to set up a "cron job", or a PHP file that runs at a certain interval you set.

Check out this article for more information.

The best part about cron jobs is that you are not limited to the small subset of functionality available in say, stored procedures. You can use whatever logic you like! :)

如果你有一个更新价格的脚本,你想要做的就是每天运行它,使用Cron (linux)或命令 (windows)。

I think that you can make a function that gets called every time a page is accessed, and verifies if the update took place, checking against a database.

So in a quick pseudo code:

function verify_often(){
  if (last_update_in_db() != today() ){
    update_db();
    run_periodic_function(); 
  }
  return 0;
}

This method requires only the classic PHP & MySQL combination.

How could you not 'have access to the command line'? I can't think of any host that doesn't allow ssh access. Having access to cron is a different story, but they SHOULD allow this, also. If they don't - find a new host!

Forget all that. There is no reason to do anything like that.

All you have to do is check each time someone calls the webpage. You keep track of the date and when the current date no longer matches your variable then you fire off the thing that gets new data. Bam! Of course that's not very scalable but it's fine for a small game.

I couldn't work out how to reply to alex's answer but I wish to mention something that they said. When they said "check how much time has passed everytime someone loads a page" I feel that you don't need to check it every time the page is loaded. The better way of doing it would be to check when a user logs in. If it goes over 24 hours while users are still logged in it will not matter for the described scenario. The next time someone logs in the prices of items will change.

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