settyping.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. $data = $_POST["datavars"];
  3. $dataarray = explode("###", $data);
  4. $user = $dataarray[0];
  5. $settyping = $dataarray[1];
  6. include("connect.php");
  7. $useristyping = false;
  8. $offlineusers = array();
  9. if( $result = $db->query("SELECT timestamp, user FROM chatpack_typing") )
  10. {
  11. while( $row = $result->fetchArray() )
  12. {
  13. $typinguser = $row["user"];
  14. $timestamp = $row["timestamp"];
  15. // check whether user is currently typing
  16. if( strcmp($typinguser, $user) == 0 )
  17. {
  18. $useristyping = true;
  19. }
  20. // catch users who are offline but still set as typing
  21. $timenow = time();
  22. if( $timestamp < $timenow - 2700 )
  23. {
  24. array_push($offlineusers, $typinguser);
  25. }
  26. }
  27. }
  28. if( !$useristyping && $settyping == 1 ) // set user as typing
  29. {
  30. $timestamp = time();
  31. $db->exec("INSERT INTO chatpack_typing (timestamp, user) VALUES ('$timestamp', '$user')");
  32. }
  33. else if( $settyping == 0 ) // set user as not typing
  34. {
  35. $db->exec("DELETE FROM chatpack_typing WHERE user='$user'");
  36. }
  37. // set offline users as not typing
  38. for( $i=0; $i<count($offlineusers); $i++ )
  39. {
  40. $db->exec("DELETE FROM chatpack_typing WHERE user='$offlineusers[$i]'");
  41. }
  42. $db->close();
  43. ?>