在服务器端执行php操作

Im projecting some web based game. Basicially i want to perform this schema:

  1. Player click on refill stock of product
  2. There is some time to needed to perform action above, for example 1 minute and 30 seconds
  3. Player change tab to product list and go back to shop overview
  4. After one hour he go back to product page and stock is refilled.

I don't know how to perform delayed php action on server side just after clicking one button by user.

I know that i can do it by simply creating another mysql table and collect there any action performed by player and create on product page simple jquery countdown with time got from sql query.

But how to do that without this table?

Also how to create some random actions such us selling product for virtual person, generate some random power cuts etc. Is it even possible with AJAX or JQuery?

Maybe i should use some cronjobs such us IPBoard one's.

Literally i don't know where to start. I know that probably i don't have skills to create some more advanced web game, but i just want to try and learn something new.

One way to achieve this, depending on your environment and permissions on the server, would be to just create a bash script and call the bash script with

exec('sh /path/to/file.sh')

from your php file, for example wrapped in an ajax call.

The bash script can contain something like

#!/bin/bash\
echo php /path/to/file.php | at now + 2 min # << or whatever time you want

If you don't want a MySQL table, I'd venture to guess you won't want to set up Redis either (it's super-simple though!) You'll also want to grab PHPRedis (also available on GitHub) Check their site for a full list of supported clients.

Redis is a simple key-value store that's optimized for speedy read/write operations, and it comes with a handy EXPIRE feature. It's great for storing small bits of data that change frequently. By setting a flag that self-deletes after n seconds, you can just check for that flag. If the flag is still present, continue show the user the "in progress" actions.

With a key of "{$user_id}:{$property}"...

<?php
$time = 90;
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('1234:item1:stock_level', 100);
$redis->setex('1234:item1:restocking_flag', $time, 1);


if ($redis->get("{$user_id}:{$item}:restocking_flag")) {
    echo "Your stock will be refilled in {$redis->ttl()} seconds";
} else {
    echo "You now have {$redis->get("{$user_id}:{$item}:stock_level")} items";
}

Of course, this is the server-side implementation. You'd want to make the call over Ajax that initiates the refill process. If you know the amount of time it takes, have a javascript timer make an ajax request after (e.g.) 90 seconds to verify with the server that the lock has been removed. Otherwise, just check as often as makes sense. This prevents tamerping on the user side, as long as your script doesn't set the Redis timeout from a direct client-side value.