active.php 1.7 KB

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