active.php 1.6 KB

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