网址缩短 短网址程序

这是一个网上的代码,无需数据库,将要缩短的网址存在于一个同目录里的txt文件里。
演示:http://www.ucmbaa.org/u/
目的一是缩短了网址,二是不让搜索引擎识别这个地址。

  1. <?php
  2. /*
  3. location of file to store URLS
  4. */
  5. $file = ‘urls.txt’;
  6. /*
  7. use mod_rewrite: 0 – no or 1 – yes
  8. */
  9. $use_rewrite = 1;
  10. /*
  11. language/style/output variables
  12. */
  13. $l_url = ‘URL’;
  14. $l_nourl = ‘没有输入URL地址‘;
  15. $l_yoururl = ‘你的短网址:‘;
  16. $l_invalidurl = ‘无效的URL.‘;
  17. $l_createurl = ‘生成短网址’;
  18. //////////////////// NO NEED TO EDIT BELOW ////////////////////
  19. if(!is_writable($file) || !is_readable($file))
  20. {
  21. die(‘Cannot write or read from file. Please CHMOD the url file (urls.txt) by default to 777 and make sure it is uploaded.’);
  22. }
  23. $action = trim($_GET[‘id’]);
  24. $action = (empty($action) || $action == ”) ? ‘create’ : ‘redirect’;
  25. $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+\$_.-]*)?\$”;
  26. $output = ”;
  27. if($action == ‘create’)
  28. {
  29. if(isset($_POST[‘create’]))
  30. {
  31. $url = trim($_POST[‘url’]);
  32. if($url == ”)
  33. {
  34. $output = $l_nourl;
  35. }
  36. else
  37. {
  38. if(eregi($valid, $url))
  39. {
  40. $fp = fopen($file, ‘a’);
  41. fwrite($fp, “{$url}\r\n”);
  42. fclose($fp);
  43. $id = count(file($file));
  44. $dir = dirname($_SERVER[‘PHP_SELF’]);
  45. $filename = explode(‘/’, $_SERVER[‘PHP_SELF’]);
  46. $filename = $filename[(count($filename) – 1)];
  47. $shorturl = ($use_rewrite == 1) ? “http://{$_SERVER[‘HTTP_HOST’]}{$dir}{$id}” : “http://{$_SERVER[‘HTTP_HOST’]}{$dir}{$filename}?id={$id}”;
  48. $output = “{$l_yoururl} {$shorturl}”;
  49. }
  50. else
  51. {
  52. $output = $l_invalidurl;
  53. }
  54. }
  55. }
  56. }
  57. if($action == ‘redirect’)
  58. {
  59. $urls = file($file);
  60. $id = trim($_GET[‘id’]) – 1;
  61. if(isset($urls[$id]))
  62. {
  63. header(“Location: {$urls[$id]}”);
  64. exit;
  65. }
  66. else
  67. {
  68. die(‘Script error’);
  69. }
  70. }
  71. //////////////////// FEEL FREE TO EDIT BELOW ////////////////////
  72. ?>
  73. 短网址服务可以帮助你把一个长网址缩短,方便你在社交网络和微博上分享链接。

  74. <form action="” method=”post”>
  75. <input type="submit" class="button" name="create" value="” />
  76. <?php
  77. ob_end_flush();
  78. ?>

复制代码

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注