u2s.php 998 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. include dirname(__FILE__)."/config.php";
  3. include dirname(__FILE__)."/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. $http = isset($_SERVER['HTTPS'])?'https':'http';
  17. // printf('<p><strong>Short URL:</strong> <a href="%s/%2$s">%2$s</a></p>',$http.'://'.$_SERVER['HTTP_HOST'], SHORTURL_PREFIX . $code);
  18. $arr = array(
  19. 'status' => 1,
  20. 'url' => $http.'://'.$_SERVER['HTTP_HOST']."/".$code,
  21. );
  22. echo json_encode($arr);
  23. exit;
  24. }
  25. catch (Exception $e) {
  26. // log exception and then redirect to error page.
  27. // header("Location: index.php");
  28. $arr = array(
  29. 'status' => 0,
  30. 'msg' => "地址有误",
  31. );
  32. echo json_encode($arr);
  33. exit;
  34. }