Advertisement
Guest User

Untitled

a guest
Apr 14th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. <html>
  2. <head>
  3. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  4. </head>
  5. <body>
  6. <script>
  7. game = {
  8. score: 0
  9. };
  10.  
  11. </script>
  12.  
  13. <?php
  14. $servername = "localhost";
  15. $username = "root";
  16. $password = "ievank";
  17. $dbname = "gametest";
  18. $user = $_SERVER['REMOTE_ADDR'];
  19.  
  20.  
  21. // Create connection
  22. $conn = new mysqli($servername, $username, $password, $dbname);
  23.  
  24. // Check connection
  25. if ($conn->connect_error) {
  26. die("Connection failed: " . $conn->connect_error);
  27. }
  28.  
  29.  
  30. $nxtquery = "SELECT * FROM players";
  31.  
  32. $result = $conn->query($nxtquery);
  33.  
  34.  
  35. $userIndex=0;
  36. $continueLoop=true;
  37. while($row = $result->fetch_assoc() && $continueLoop) {
  38. $resarray = $result->fetch_assoc();
  39. if($resarray["ip"]===$user){
  40. echo "<script id='tempScript'>game.score=" . $resarray["score"] . "; game.user='" . $user . "'</script>";
  41. $continueLoop=false;
  42. } else {
  43. $userIndex++;
  44. }
  45. }
  46. if($continueLoop) {
  47. $nxtquery = "INSERT INTO players (ip, score) VALUES ('" . $_SERVER['REMOTE_ADDR'] . "', '0')";
  48. $conn->query($nxtquery);
  49. }
  50.  
  51.  
  52. ?>
  53.  
  54. <script>
  55. document.getElementById("tempScript").remove();
  56. function SubForm(){
  57. formData= 'score='+game.score+'&user='+game.user;
  58. $.ajax({
  59. url:'sendScore.php',
  60. type:'post',
  61. data: formData,
  62. success:function(){
  63. console.log("saved");
  64. }
  65. });
  66. }
  67.  
  68. window.setInterval(function(){
  69. SubForm();
  70. }, 15000);
  71. </script>
  72.  
  73. </body>
  74. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement