function addNewURL($mysqli, $destURL) { $destURL = urlencode($destURL); // Generate a unique short URL ID $shortID = ""; do { $shortID = generateShortID(); } while (shortIDExists($mysqli, $shortID)); // Insert the short ID and the destination URL into the db. $query = "INSERT INTO tbl_url values(?, ?, NULL)"; if ($stmt = $mysqli->prepare($query)) { $stmt->bind_param("ss", $shortID, $destURL); $stmt->execute(); return $shortID; } }
if (empty($_GET["id"])) { header("location: /"); die; } $mysqli = connectToDB(); if ($mysqli->connect_errno) { die("Faild to connect to database:" . PHP_EOL . $mysqli->connect_errno . PHP_EOL . $mysqli->connect_error . PHP_EOL); } $shortID = htmlspecialchars($_GET["id"], ENT_QUOTES); $hasInfoChar = hasInfoChar($shortID); if ($hasInfoChar) { // Remove the info char ('+') from the shortID. $shortID = substr($shortID, 0, strlen($shortID) - 1); } $shortIDExists = shortIDExists($mysqli, $shortID); if (!shortIDExists($mysqli, $shortID)) { header("location: /"); $mysqli->close(); die; } // store the information for the shortID. $url = getLongURL($mysqli, $shortID); $creationDate = getCreationDate($mysqli, $shortID); ?> <!DOCTYPE html> <html lang="us"> <head> <meta charset="utf-8"> <link type="text/css" rel="stylesheet" href="css/main.css"/> <script src="http://code.jquery.com/jquery-latest.min.js"