这是一个网上的代码,无需数据库,将要缩短的网址存在于一个同目录里的txt文件里。 演示:http://www.ucmbaa.org/u/ 目的一是缩短了网址,二是不让搜索引擎识别这个地址。
-
- /*
- location of file to store URLS
- */
- $file = 'urls.txt';
-
- /*
- use mod_rewrite: 0 - no or 1 - yes
- */
- $use_rewrite = 1;
-
- /*
- language/style/output variables
- */
-
- $l_url = 'URL';
- $l_nourl = '没有输入URL地址';
- $l_yoururl = '你的短网址:';
- $l_invalidurl = '无效的URL.';
- $l_createurl = '生成短网址';
-
- //////////////////// NO NEED TO EDIT BELOW ////////////////////
-
- if(!is_writable($file) || !is_readable($file))
- {
- die('Cannot write or read from file. Please CHMOD the url file (urls.txt) by default to 777 and make sure it is uploaded.');
- }
-
- $action = trim($_GET['id']);
- $action = (empty($action) || $action == '') ? 'create' : 'redirect';
-
- $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+\$_.-]*)?\$";
-
- $output = '';
-
- if($action == 'create')
- {
- if(isset($_POST['create']))
- {
- $url = trim($_POST['url']);
-
- if($url == '')
- {
- $output = $l_nourl;
- }
- else
- {
- if(eregi($valid, $url))
- {
- $fp = fopen($file, 'a');
- fwrite($fp, "{$url}\r\n");
- fclose($fp);
-
- $id = count(file($file));
- $dir = dirname($_SERVER['PHP_SELF']);
- $filename = explode('/', $_SERVER['PHP_SELF']);
- $filename = $filename[(count($filename) - 1)];
-
- $shorturl = ($use_rewrite == 1) ? "http://{$_SERVER['HTTP_HOST']}{$dir}{$id}" : "http://{$_SERVER['HTTP_HOST']}{$dir}{$filename}?id={$id}";
-
- $output = "{$l_yoururl} {$shorturl}";
- }
- else
- {
- $output = $l_invalidurl;
- }
- }
- }
- }
-
- if($action == 'redirect')
- {
- $urls = file($file);
- $id = trim($_GET['id']) - 1;
- if(isset($urls[$id]))
- {
- header("Location: {$urls[$id]}");
- exit;
- }
- else
- {
- die('Script error');
- }
- }
-
- //////////////////// FEEL FREE TO EDIT BELOW ////////////////////
- ?>
-
-
短网址服务可以帮助你把一个长网址缩短,方便你在社交网络和微博上分享链接。
-
-
-
-
|