Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- table, th, td {
- border: 1px solid black;
- border-collapse: collapse;
- }
- th, td {
- padding: 5px;
- }
- </style>
- </head>
- <body>
- <?php
- $servername = "localhost";
- $username = "[USERNAME_HERE]";
- $password = "[PASSWORD_HERE]";
- $dbname = "[DATABASE_NAME_HERE]";
- $table_name = "mc_server_ip";
- //create connection
- $conn = new mysqli($servername, $username, $password, $dbname);
- //check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- if ($_SERVER["REQUEST_METHOD"] === "POST") {
- //handle writing new address to database
- //get POST data
- $ipset = isset($_POST["ipaddr"]) && !empty($_POST["ipaddr"]);
- $portset = isset($_POST["port"]) && !empty($_POST["port"]);
- $tokenset = isset($_POST["token"]) && !empty($_POST["token"]);
- if ($ipset && $portset && $tokenset) {
- //data exists, fetch it
- $ipaddr = $_POST["ipaddr"];
- $token = $_POST["token"];
- $port = $_POST["port"];
- //check data
- $expected_token = "[EXPECTED_TOKEN_HERE]";
- $ip_good = preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ipaddr);
- $port_good = preg_match("/^\d{1,8}$/", $port);
- if($ip_good != 1 || $token != $expected_token || $port_good != 1) {
- if($ip_good != 1) {
- echo "invalid IP address format <br />";
- }
- if($token != $expected_token) {
- echo "invalid or missing token <br/>";
- }
- if($port_good != 1) {
- echo "invalid port format";
- }
- }
- else {
- //add data to db
- $sql = "INSERT INTO " . $table_name . " (ip_address, port) VALUES ('" . $ipaddr . "', '" . $port . "');";
- if($conn->query($sql) === TRUE) {
- echo "inserted successfully";
- // remove previous entries with the same address and port from the table
- $sql = "SELECT MAX(id) AS id, ip_address, port
- FROM " . $table_name .
- " GROUP BY ip_address, port
- ORDER BY MAX(date_received) DESC;";
- $result = $conn->query($sql);
- if ($result->num_rows > 0) {
- while($row = $result->fetch_assoc()) {
- $sql = "DELETE FROM " . $table_name . " WHERE ip_address = '" . $row["ip_address"]
- . "' AND id < " . $row["id"] . " AND port = '" . $row["port"] . "' ;";
- $conn->query($sql);
- }
- }
- }
- else {
- echo "insert failed";
- }
- } //if post values are valid
- } //if all post values are set
- else {
- echo "one or more input fields are missing: ipaddr, port, token";
- }
- }
- else {
- // GET request, show latest IP. or more if they specify a "count" parameter
- $count = 1;
- if(isset($_GET["count"]) && !empty($_GET["count"])) {
- $tempcount = $_GET["count"];
- if(preg_match("/^\d+$/", $tempcount) == 1) {
- $count = $tempcount;
- }
- }
- $sql = "SELECT MAX(date_received) AS date_received, ip_address, port
- FROM " . $table_name .
- " GROUP BY ip_address, port
- ORDER BY MAX(date_received) DESC
- LIMIT " . $count . ";";
- $result = $conn->query($sql);
- if($result->num_rows > 0) {
- //output data of each row
- echo "<table>";
- echo "<tr><th>Date Received</th> <th>IP Address</th></tr>";
- while($row = $result->fetch_assoc()) {
- echo "<tr><td>" . $row["date_received"] . "</td><td>" . $row["ip_address"] . ":"
- . $row["port"] . "</td></tr>";
- }
- echo "</table>";
- }
- else {
- echo "No IP addresses saved";
- }
- }
- $conn->close();
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement