views:

338

answers:

10
+2  Q: 

PHP game update

Hi,

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

+8  A: 

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.

+3  A: 

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! :)

JoshJordan
+2  A: 

If you have a script that updates the prices and all you want to do is run it every day, use Cron (linux) or at command (windows).

ya23
+4  A: 

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.

Ikke
Ahh, so this is a method that only updates when a user loads the page, am I right? This is what I have tried to not use.
Ryan
Well, if you don't have access to cron or something like, you don't have many alternatives.
Ikke
Why would you do this? Since you're calling a PHP script, you can obviously run PHP on the server - just include('./cron.php') where that file is a basic cron-emulator...
Michael Wales
+1  A: 

I think that you can make a fuction that gets called everytime a page is accessed, and verifies if the update took place, checking against a database. So in a quick pseudocode

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.

adrians
+3  A: 

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.

+1  A: 

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!

Alex JL
+3  A: 

Use webcron :) http://www.webcron.org/index.php?lang=en

Or here is a good list: http://www.onlinecronservices.com/

Thinker
+3  A: 

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.

Kludge
A: 

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.

david jordan