URL shortened short URL program

Source: Internet
Author: User

This is an online code, no database, a shortened URL exists in a TXT file in the same directory.
Demo: http://www.ucmbaa.org/u/
The goal is to shorten the Web site, the second is not to allow search engines to identify this address.

  1. /*
  2. Location of the file to store URLS
  3. */
  4. $file = ' urls.txt ';
  5. /*
  6. Use mod_rewrite:0-No or 1-yes
  7. */
  8. $use _rewrite = 1;
  9. /*
  10. Language/style/output variables
  11. */
  12. $l _url= ' url ';
  13. $l _nourl= ' did not enter a URL address ';
  14. $l _yoururl= ' your short URL:';
  15. $l _invalidurl= ' invalid URL.';
  16. $l _createurl= ' generate short URLs ';
  17. NO need to EDIT BELOW////////////////////
  18. if (!is_writable ($file) | |!is_readable ($file))
  19. {
  20. Die (' cannot write or read from file. Please CHMOD the URL of file (urls.txt) by default to 777 and make sure it is uploaded. ');
  21. }
  22. $action = Trim ($_get[' id ');
  23. $action = (Empty ($action) | | $action = = ")? ' Create ': ' redirect ';
  24. $valid = "^ (https?| FTP) \:\/\/([a-z0-9+!* (),;? &=\$_.-]+ (\:[a-z0-9+!* (),;? &=\$_.-]+)? @)? [A-z0-9+\$_-]+ (\.[ a-z0-9+\$_-]+) * (\:[0-9]{2,5})? (\ \ [a-z0-9+\$_-]\.?) +)*\/? (\? [a-z+&\$_.-] [a-z0-9;:@/&%=+\$_.-]*]? (#[a-z_.-][a-z0-9+\$_.-]*)? \$ ";
  25. $output = ";
  26. if ($action = = ' Create ')
  27. {
  28. if (Isset ($_post[' create '))
  29. {
  30. $url = Trim ($_post[' url ');
  31. if ($url = = ")
  32. {
  33. $output = $l _nourl;
  34. }
  35. Else
  36. {
  37. if (eregi ($valid, $url))
  38. {
  39. $fp = fopen ($file, ' a ');
  40. Fwrite ($fp, "{$url}\r\n");
  41. Fclose ($FP);
  42. $id = count (file ($file));
  43. $dir = dirname ($_server[' php_self ');
  44. $filename = explode ('/', $_server[' php_self ');
  45. $filename = $filename [(Count ($filename)-1)];
  46. $shorturl = ($use _rewrite = = 1)? "Http://{$_server[' Http_host ']}{$dir} {$id}": "Http://{$_server[' Http_host ']}{$dir} {$filename}?id={$id}";
  47. $output = "{$l _yoururl} {$shorturl}";
  48. }
  49. Else
  50. {
  51. $output = $l _invalidurl;
  52. }
  53. }
  54. }
  55. }
  56. if ($action = = ' redirect ')
  57. {
  58. $urls = file ($file);
  59. $id = Trim ($_get[' id ')-1;
  60. if (Isset ($urls [$id]))
  61. {
  62. Header ("Location: {$urls [$id]}");
  63. Exit
  64. }
  65. Else
  66. {
  67. Die (' Script error ');
  68. }
  69. }
  70. Feel free to EDIT BELOW////////////////////
  71. ?>
  72. The short URL service can help you shorten a long URL, so it's easy to share links on social networks and Weibo.

  73. Ob_end_flush ();
  74. ?>
Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.