-1
$timeNow = time();
$timeExpire = $timeNow + 10;
$User = $con->query("SELECT * FROM players WHERE Username='".$_SESSION['Username']."'");
$UserInfo = $User->fetch_object();

if($timeNow >= $UserInfo->TimeExpire) {

    echo "Wood Gathered";
    $con->query("UPDATE players SET Wood = Wood+5 WHERE Username='".$_SESSION['Username']."'");
    $con->query("UPDATE players SET Time = '".$timeNow."' WHERE Username='".$_SESSION['Username']."'");
    $con->query("UPDATE players SET TimeExpire = '".$timeExpire."' WHERE Username='".$_SESSION['Username']."'");

}

How can I make the following code work even the user is not logged in?

Like for example he comes back in 1 day and gets 1728 wood because 86400 (1 day) / 50 (get resources every 50 seconds) is equal to 1728.

4

1 回答 1

0

I'm sensing you're programing a game and since games need to be real-time: Create a file e.g cron.php and include that file in every page. Inside that file put this:

<?php
$timeNow = time();
$timeExpire = $timeNow + 10;
$Users = $con->query("SELECT * FROM players WHERE TimeExpire<='".$timeNow."'");
while($UserInfo = $User->fetch_object()){
    $givenWood = $timeNow - $UserInfo->TimeExpire;
    $con->query("UPDATE players SET Wood = Wood+ {$givenWood}   WHERE Username='". $UserInfo->Username ."'");
    $con->query("UPDATE players SET Time = '".$timeNow."' WHERE Username='". $UserInfo->Username ."'");
    $con->query("UPDATE players SET TimeExpire = '".$timeExpire."' WHERE Username='". $UserInfo->Username ."'");
}

?>

Have in mind that I put this code just to give the idea how to do that.So, there might be something wrong with my logic in the code. (What you should do is get those users whom you can give some wood to them, calculate them time between now and last time you gave them their wood and give them proper amount of wood).

于 2013-09-10T23:09:09.173 回答