Home > Backend Development > PHP Tutorial > URL shortener URL shortener

URL shortener URL shortener

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-25 09:07:53
Original
1167 people have browsed it

This is an online code that does not require a database. The URL to be shortened is stored in a txt file in the same directory.
Demonstration: http://www.ucmbaa.org/u/
The first purpose is to shorten the URL, and the second is to prevent search engines from recognizing this address.

  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 = 'No URL address entered';
  14. $l_yoururl = 'Your short URL:';
  15. $l_invalidurl = 'Invalid URL.';
  16. $l_createurl = ' Generate short URL';
  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}rn");
  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, making it easier for you to share the link on social networks and Weibo.

  73. < p class="response">

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template