active.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. //统计用户经验值
  3. require_once '../path.php';
  4. require_once "../public/function.php";
  5. define("MAX_INTERVAL",600000);
  6. if(isset($_COOKIE["userid"])){
  7. $dns = "sqlite:"._FILE_DB_USER_ACTIVE_;
  8. $dbh = new PDO($dns, "", "",array(PDO::ATTR_PERSISTENT=>true));
  9. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  10. // 查询上次编辑活跃结束时间
  11. $query = "SELECT id, end, start,hit FROM edit WHERE user_id = ? order by end DESC";
  12. $stmt = $dbh->prepare($query);
  13. $stmt->execute(array($_COOKIE["userid"]));
  14. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  15. $new_record = false;
  16. $currTime = mTime();
  17. if ($row) {
  18. //找到,判断是否超时,超时新建,未超时修改
  19. $endtime = (int)$row["end"];
  20. $id = (int)$row["id"];
  21. $start_time = (int)$row["start"];
  22. $hit = (int)$row["hit"];
  23. if($currTime-$endtime>MAX_INTERVAL){
  24. //超时新建
  25. $new_record = true;
  26. }
  27. else{
  28. //未超时修改
  29. $new_record = false;
  30. }
  31. } else {
  32. //没找到,新建
  33. $new_record = true;
  34. }
  35. if($new_record){
  36. $query="INSERT INTO edit ( user_id, start , end , duration , hit ) VALUES ( ? , ? , ? , ? , ? ) ";
  37. $sth = $dbh->prepare($query);
  38. $sth->execute(array($_COOKIE["userid"] , $currTime , $currTime , 10000,1) );
  39. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  40. $error = $dbh->errorInfo();
  41. }
  42. }
  43. else{
  44. $query="UPDATE edit SET end = ? , duration = ? , hit = ? WHERE id = ? ";
  45. $sth = $dbh->prepare($query);
  46. $sth->execute( array($currTime,($currTime-$start_time), ($hit+1),$id));
  47. if (!$sth || ($sth && $sth->errorCode() != 0)) {
  48. $error = $dbh->errorInfo();
  49. }
  50. }
  51. }
  52. ?>