123456789101112131415161718192021222324252627 |
- <?php
- include "./config.php";
- include "./ShortUrl.php";
- try {
- $pdo = new PDO(DB_PDODRIVER . ":host=" . DB_HOST .
- ";dbname=" . DB_DATABASE,
- DB_USERNAME, DB_PASSWORD);
- }
- catch (PDOException $e) {
- trigger_error("Error: Failed to establish connection to database.");
- exit;
- }
-
- $shortUrl = new ShortUrl($pdo);
- try {
- $code = $shortUrl->urlToShortCode($_POST["url"]);
- printf('<p><strong>Short URL:</strong> <a href="%s/%2$s">%2$s</a></p>',$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'],
- SHORTURL_PREFIX . $code);
- exit;
- }
- catch (Exception $e) {
- // log exception and then redirect to error page.
- // header("Location: /error");
- header("Location: index.php");
- exit;
- }
|