Advertisement
Guest User

Untitled

a guest
Oct 7th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. table, th, td {
  6. border: 1px solid black;
  7. border-collapse: collapse;
  8. }
  9. th, td {
  10. padding: 5px;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <?php
  16. $servername = "localhost";
  17. $username = "[USERNAME_HERE]";
  18. $password = "[PASSWORD_HERE]";
  19. $dbname = "[DATABASE_NAME_HERE]";
  20.  
  21. $table_name = "mc_server_ip";
  22.  
  23. //create connection
  24. $conn = new mysqli($servername, $username, $password, $dbname);
  25. //check connection
  26. if ($conn->connect_error) {
  27. die("Connection failed: " . $conn->connect_error);
  28. }
  29.  
  30. if ($_SERVER["REQUEST_METHOD"] === "POST") {
  31. //handle writing new address to database
  32.  
  33. //get POST data
  34. $ipset = isset($_POST["ipaddr"]) && !empty($_POST["ipaddr"]);
  35. $portset = isset($_POST["port"]) && !empty($_POST["port"]);
  36. $tokenset = isset($_POST["token"]) && !empty($_POST["token"]);
  37. if ($ipset && $portset && $tokenset) {
  38. //data exists, fetch it
  39. $ipaddr = $_POST["ipaddr"];
  40. $token = $_POST["token"];
  41. $port = $_POST["port"];
  42.  
  43. //check data
  44. $expected_token = "[EXPECTED_TOKEN_HERE]";
  45. $ip_good = preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ipaddr);
  46. $port_good = preg_match("/^\d{1,8}$/", $port);
  47. if($ip_good != 1 || $token != $expected_token || $port_good != 1) {
  48. if($ip_good != 1) {
  49. echo "invalid IP address format <br />";
  50. }
  51. if($token != $expected_token) {
  52. echo "invalid or missing token <br/>";
  53. }
  54. if($port_good != 1) {
  55. echo "invalid port format";
  56. }
  57. }
  58. else {
  59. //add data to db
  60. $sql = "INSERT INTO " . $table_name . " (ip_address, port) VALUES ('" . $ipaddr . "', '" . $port . "');";
  61. if($conn->query($sql) === TRUE) {
  62. echo "inserted successfully";
  63.  
  64. // remove previous entries with the same address and port from the table
  65. $sql = "SELECT MAX(id) AS id, ip_address, port
  66. FROM " . $table_name .
  67. " GROUP BY ip_address, port
  68. ORDER BY MAX(date_received) DESC;";
  69. $result = $conn->query($sql);
  70. if ($result->num_rows > 0) {
  71. while($row = $result->fetch_assoc()) {
  72. $sql = "DELETE FROM " . $table_name . " WHERE ip_address = '" . $row["ip_address"]
  73. . "' AND id < " . $row["id"] . " AND port = '" . $row["port"] . "' ;";
  74. $conn->query($sql);
  75. }
  76. }
  77. }
  78. else {
  79. echo "insert failed";
  80. }
  81. } //if post values are valid
  82. } //if all post values are set
  83. else {
  84. echo "one or more input fields are missing: ipaddr, port, token";
  85. }
  86. }
  87. else {
  88. // GET request, show latest IP. or more if they specify a "count" parameter
  89. $count = 1;
  90. if(isset($_GET["count"]) && !empty($_GET["count"])) {
  91. $tempcount = $_GET["count"];
  92. if(preg_match("/^\d+$/", $tempcount) == 1) {
  93. $count = $tempcount;
  94. }
  95. }
  96.  
  97. $sql = "SELECT MAX(date_received) AS date_received, ip_address, port
  98. FROM " . $table_name .
  99. " GROUP BY ip_address, port
  100. ORDER BY MAX(date_received) DESC
  101. LIMIT " . $count . ";";
  102. $result = $conn->query($sql);
  103.  
  104. if($result->num_rows > 0) {
  105. //output data of each row
  106. echo "<table>";
  107. echo "<tr><th>Date Received</th> <th>IP Address</th></tr>";
  108. while($row = $result->fetch_assoc()) {
  109. echo "<tr><td>" . $row["date_received"] . "</td><td>" . $row["ip_address"] . ":"
  110. . $row["port"] . "</td></tr>";
  111. }
  112. echo "</table>";
  113. }
  114. else {
  115. echo "No IP addresses saved";
  116. }
  117. }
  118.  
  119. $conn->close();
  120. ?>
  121. </body>
  122.  
  123. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement