我尝试在用户登录时设置在线时间和硬币。
用户必须获得1个硬币。小时他们在线,我已经计算了在线时间的秒数,但我怎样才能正确计算硬币?
也许它必须每35秒更新一次硬币计数?
<?php
session_start();
// Set the session if not set.
if ( !isset ( $_SESSION['startOnlineTime'])):
$_SESSION['startOnlineTime'] = time();
endif;
$secondsOnline = (time() - $_SESSION['startOnlineTime']);
?>
我尝试过(没有运气):'
<?php
//if updates every 35 secs, then i calculate it like: 1 (per hour) / 35 (seconds) - 1/35? correct?
$prHour = (Double) 0.028571428571429;
$SQL = "UPDATE users SET coins=coins+0.15 WHERE id = '@session_id()'";
mysql_query($SQL) or die ( mysql_error() );
?>
但似乎不正确。我该怎么办?
答案 0 :(得分:0)
这可能是解决您问题的方法之一。创建一个单独的php文件调用counter.php
,内容如下。这个文件将定期调用,在本例中为每小时调用。
<强> counter.php 强>
<?php
if(session_id() == '') {
sesstion_start();
if (!isset($_SESSION['userid'])
return;
// I use 'userid' to save the user info, you can use others like ip, session_id, ...
// verify that this file is called hourly
$t = time();
if (isset($_SESSION['lastcheck']) && $t - $_SESSION['lastcheck'] < 3550)
return;
// if this script is call in the previous 1 hour (3600s), ignore this call.
$_SESSION['lastcheck'] = time();
/* update your database here, increment 1 coin in this user record */
}
?>
在您希望向用户收费的每个页面中,使用JavaScrip每小时调用counter.php
。这个例子使用jQuery库来帮助:
function charge() {
var waitingTimes = 3600; //1 hours
$.get("absolute/url/to/counter.php");
setInterval(charge,waitingTimes)
}
$(document).ready(function(){
charge();
});