網址縮短 短網址程式

來源:互聯網
上載者:User

這是一個網上的代碼,無需資料庫,將要縮短的網址存在於一個同目錄裡的txt檔案裡。
示範:http://www.ucmbaa.org/u/
目的一是縮短了網址,二是不讓搜尋引擎識別這個地址。

  1. /*
  2. location of 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= '沒有輸入URL地址';
  14. $l_yoururl= '你的短網址:';
  15. $l_invalidurl= '無效的URL.';
  16. $l_createurl= '產生短網址';
  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 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. 短網址服務可以協助你把一個長網址縮短,方便你在社交網路和微博上分享連結。

  73. ob_end_flush();
  74. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.