0

It's bugging me for a day now and I really cant find out, I have a basic login/register page, and when registering, a timestamp is stored in the mysql database(table timer, column cooldown):

$settime = mysql_query("INSERT INTO `timer` (`cooldown`) VALUES(0)") or die(mysql_error());

What I want to do now (I'm creating a browser mmorpg), Is when I do a specific POST request, I want a timer in my database to go off. This timer should be 1 minute, and also be shown for users, like: <?php echo $timer['cooldown']; ?> Whenever the timer is = 0, I can do a specific function again, and the timer will be set to 60 seconds again.

Sorry for the lack of knowledge but I can't find out anywhere how to do this.

5
  • 1
    Before you start creating something as intricate a a game, start by reading up on mysqli_* or PDO... the mysql_* functions are deprecated. Commented Sep 19, 2013 at 14:17
  • a timer to go off in your database? I believe you are looking for some jQuery timer calling different functions maybe ajax or post but I recommend to handle the timer on the client side Commented Sep 19, 2013 at 14:19
  • Well, I'm open for other possibilities, but having a 'cooldown' in Javascript just reset if I open another page,right? Wouldn't it be better to have it stored in the DB linked to the user ID? Commented Sep 19, 2013 at 14:24
  • AFAIK this cannot be done. Action on server can be done only after action send by user. You can try using CLI. It automatically runs php script on server every X minutes, but doesn't answer your problem directly (will not be activated by event). Commented Sep 19, 2013 at 14:33
  • You can try to calculate how much time have passed since last user action and calculate how many 'ticks' (your 60 sec intervals) have passed, and then set data correctly (eg. user action after 200 sec = 3 'ticks' + 20 seconds) so player 'gold' is increased by 3*(amount of gold every 60 sec) and you set timer to -20 seconds to compensate. Don't know if it's how they are doing it, but i have no better solution Commented Sep 19, 2013 at 14:39

1 Answer 1

1

What you're trying to do here - a background job - goes against the web development principle of a request-response cycle in the shared-nothing environment of PHP. But there are several ways to break up the rigid cycle:

  • If you just need to do some DB updates after 1 minute, you can use MySQL events: http://dev.mysql.com/doc/refman/5.1/en/events.html
  • If the function you want to call is not running too long, you can check on every user request if there are entries in the timer table that are older than 1 minute.
  • Create a PHP script that is called by a cron job every minute. It checks if there are unhandled items in the timer table and does something with them.
  • Create a PHP daemon script that wakes up every minute to check.
  • If you need to change something on the user page after 1 minute, doing the PHP script call client-side with a JavaScript timeout and AJAX or websockets is the better option.

For displaying the countdown to the user, you have to use JavaScript. If you are using the server-side timer, you can use it just for display purposes, hiding the countdown when it's finished. To work around the "user opens new page before timout is finished" problem, put the data for the remaining seconds in an HTML data attribute where your JavaScript code can read it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.