connect_error) { die("Connection failed: " . $conn->connect_error); } function sanitise_data($data) { global $conn; $data = mysqli_real_escape_string($conn, $data); $data = htmlspecialchars($data, ENT_QUOTES, 'UTF-8'); return $data; } function writeToDB($sour, $dest) { global $conn; $sql = "INSERT INTO url (source, destination) VALUES ('$sour', '$dest')"; if (mysqli_query($conn, $sql)) { //echo "Wrote " . $sour . " => " . $dest . " to database"; } else { echo "Error: " . $sql . "
" . mysqli_error($conn); } } $sql = "SHOW TABLES LIKE 'url'"; $result = $conn->query($sql); $db_installed = $result->num_rows; if ($db_installed == 0){ //install db $sql = "CREATE TABLE url (id INT(8) UNSIGNED AUTO_INCREMENT PRIMARY KEY, source VARCHAR(60), destination VARCHAR(1024))"; if (mysqli_query($conn, $sql)) { writeToDB("favicon.ico", "https://git.speerfamily.ie/uploads/project/avatar/1/compress.png"); } else { echo "Error creating table: " . mysqli_error($conn); } } function getCurrentUri() { $basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/'; $uri = substr($_SERVER['REQUEST_URI'], strlen($basepath)); if (strstr($uri, '?')) $uri = substr($uri, 0, strpos($uri, '?')); $uri = '/' . trim($uri, '/'); return $uri; } $base_url = getCurrentUri(); $routes = array(); $routes = explode('/', $base_url); $addr = array(); foreach($routes as $route) { if(trim($route) != '') array_push($routes, $route); if ($route != ""){ $addr[] = $route; } } $source = sanitise_data($_POST["source"]); $destination = sanitise_data($_POST["destination"]); $reload = $_POST["reload"]; if ($addr[0] == ""){ if($source == "" && $destination == "" && $reload == ""){ $view = "form"; } elseif($destination == ""){ $error= "no-destination"; $view = "form"; } elseif($source != ""){ $sql = "SELECT source FROM url WHERE source = '$source'"; $result = $conn->query($sql); if ($result->num_rows > 0) { $view = "form"; $error = "source-taken"; } else { writeToDB($source, $destination); $view = "success"; } } else{ $view = "success"; $error = "source-taken"; while ($error == "source-taken"){ $source = sanitise_data(rtrim(strtr(base64_encode(openssl_random_pseudo_bytes($source_length)), '+/', '-_'), '=')); $sql = "SELECT source FROM url WHERE source = '$source'"; $result = $conn->query($sql); if ($result->num_rows > 0) { $error = "source-taken"; $view = "form"; } else { writeToDB($source, $destination); $error = ""; $view = "success"; } } } } else{ $view = "blank"; $url0 = sanitise_data($addr[0]); $sql = "SELECT destination FROM url WHERE source = '$url0'"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $destination = $row["destination"]; } if (0 === strpos($destination, 'http')) { header('Location: ' . $destination); } else{ header('Location: http://' . $destination); } } else { $view = "form"; } } //*******View*******\\ ?> Simple URL Shortener
/

The URL " . $site_url . "/" . $source . " redirects to " . $destination . "
"; ?>