u2s.php 729 B

123456789101112131415161718192021222324252627
  1. <?php
  2. include "./config.php";
  3. include "./ShortUrl.php";
  4. try {
  5. $pdo = new PDO(DB_PDODRIVER . ":host=" . DB_HOST .
  6. ";dbname=" . DB_DATABASE,
  7. DB_USERNAME, DB_PASSWORD);
  8. }
  9. catch (PDOException $e) {
  10. trigger_error("Error: Failed to establish connection to database.");
  11. exit;
  12. }
  13. $shortUrl = new ShortUrl($pdo);
  14. try {
  15. $code = $shortUrl->urlToShortCode($_POST["url"]);
  16. printf('<p><strong>Short URL:</strong> <a href="%s/%2$s">%2$s</a></p>',$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'],
  17. SHORTURL_PREFIX . $code);
  18. exit;
  19. }
  20. catch (Exception $e) {
  21. // log exception and then redirect to error page.
  22. // header("Location: /error");
  23. header("Location: index.php");
  24. exit;
  25. }