<?php
$host = "localhost";
$user = "root";
$password = "";
$dbname = "mysql";
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$content = $_POST["text"];
$id = uniqid();
$conn = new mysqli($host, $user, $password, $dbname);
$stmt = $conn->prepare("INSERT INTO pastebox (id, ts, content) VALUES (?, UNIX_TIMESTAMP(), ?)");
$stmt->bind_param("ss", $id, $content);
$stmt->execute();
$stmt->close();
$conn->close();
echo $id;
} elseif (isset($_GET["id"])) {
$id = $_GET["id"];
$conn = new mysqli($host, $user, $password, $dbname);
$stmt = $conn->prepare("SELECT content FROM pastebox WHERE id = ? AND ts >= UNIX_TIMESTAMP() - 86400");
$stmt->bind_param("s", $id);
$stmt->execute();
$stmt->bind_result($content);
$stmt->fetch();
$stmt->close();
$conn->close();
echo $content;
} else echo '
<textarea id="t" style="height: 300px; width: 100%; resize: none; margin-bottom: 30px;"></textarea>
<input id="c" type="submit"><p id="p"></p>
<script>
const t = document.getElementById("t")
const c = document.getElementById("c")
const p = document.getElementById("p")
c.addEventListener("click", () => {
const text = t.value.trim()
if (!text) return
c.disabled = true
fetch("", { method: "POST", body: new URLSearchParams({ text }) })
.then(response => response.text().then(id => {
c.disabled = false
t.value = ""
p.textContent = window.location.href + "?id=" + id
}))
})
</script>
';