shorten.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. require_once 'config.php';
  3. $url = $_GET['url'];
  4. error_reporting(E_ALL);
  5. if (!preg_match("/(http|https):\/\/(.*?)$/i", $_GET['url'])) {
  6. echo '<h3 style="color:#FF0000;">Invalid url should start with http:// or https:// </h3>';
  7. } elseif ($_GET['url'] == 'http://' || $_GET['url'] == 'https://') {
  8. echo '<h3 style="color:#FF0000;">Invalid url</h3>';
  9. } else {
  10. try {
  11. $db = new PDO("sqlite:$sqlitedb") or die("fail to connect db");
  12. }
  13. catch (Exception $e) {
  14. die($e);
  15. }
  16. try {
  17. $stmt = $db->prepare("INSERT INTO main (id, url, shortened) VALUES (:id, :urlinput, :shorturl);");
  18. $stmt->bindParam(':id', $id);
  19. $stmt->bindParam(':urlinput', $urlinput);
  20. $stmt->bindParam(':shorturl', $shorturl);
  21. $id = rand(10000, 99999);
  22. $urlinput = $_GET['url'];
  23. $shorturl = base_convert($id, 20, 36);
  24. $stmt->execute();
  25. }
  26. catch (Exception $e) {
  27. die($e);
  28. }
  29. echo '<h3 style="color:#228B22;">Your url is:
  30. <a href="' . $_GET['url'] . '" target="_blank">' . $_GET['url'] . '</a></h3>';
  31. echo '<h3 style="color:#228B22;">Your Short url is:
  32. <a href="' . "$domain/$shorturl" . '" target="_blank">' . "$shorturl" . '</a></h3><br/>';
  33. $db = null;
  34. }
  35. ?>