用户未登录时更新

时间:2013-09-10 22:51:30

标签: php mysql

$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']."'");

}

即使用户未登录,如何使以下代码正常工作?

例如,他在1天内回来并得到1728木,因为86400(1天)/ 50(每50秒获得一次资源)等于1728。

1 个答案:

答案 0 :(得分:0)

我感觉你正在编写游戏,因为游戏需要是实时的: 创建一个文件,例如cron.php,并在每个页面中包含该文件。在该文件中放置了这个:

<?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 ."'");
}

?>

请记住,我把这段代码只是为了说明如何做到这一点。所以,我的代码中的逻辑可能有问题。 (你应该做的是让那些你可以给他们一些木头的用户,计算他们从现在到你最后一次给他们木材并给他们适当数量木材的时间。)