├── favicon.ico ├── .htaccess ├── redirect.php ├── shorten.php ├── index.php ├── config.php ├── README.md ├── url.sql ├── functions.php └── page.html /favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/littleplus/url-shorter/HEAD/favicon.ico -------------------------------------------------------------------------------- /.htaccess: -------------------------------------------------------------------------------- 1 | RewriteEngine On 2 | RewriteBase / 3 | RewriteCond %{REQUEST_FILENAME} !-f 4 | RewriteCond %{REQUEST_FILENAME} !-d 5 | RewriteRule . /index.php [L] 6 | -------------------------------------------------------------------------------- /redirect.php: -------------------------------------------------------------------------------- 1 | 0,'ukey'=>null]; 5 | 6 | $ukey=urlInsert(['url'=>$url]); 7 | if(!$ukey){ 8 | $result['error']=1; 9 | } 10 | else{ 11 | $result['ukey']=$ukey; 12 | } 13 | 14 | jsonRender($result); 15 | } 16 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 8 | } catch (Exception $e) { 9 | if(DEBUG_MODE)die($e->getMessage()); 10 | die('Fail to Connect the Database.'); 11 | } 12 | 13 | // Insert URL to database | 插入链接到数据库 14 | function urlInsert($params){ 15 | $url=$params['url'].''; 16 | $host=parse_url($url)['host']; 17 | if(empty($host)){ 18 | $host=parse_url('http://'.$url)['host']; 19 | $url='http://'.$url; 20 | } 21 | $ip=trim(end(explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']))); 22 | 23 | do{ 24 | $ukey=ukeyGen(); 25 | }while(ukeyCheck($ukey)); 26 | 27 | global $dbc; 28 | try{ 29 | $stmt = $dbc->prepare("INSERT INTO `".DB_TABLE."` (`domain`, `url`, `ukey`, `ip`) VALUES 30 | (:host, :url, :ukey, :ip)"); 31 | $stmt->bindParam(':host',$host); 32 | $stmt->bindParam(':url',$url); 33 | $stmt->bindParam(':ukey',$ukey); 34 | $stmt->bindParam(':ip',$ip); 35 | $stmt->execute(); 36 | 37 | if($stmt->rowCount()){ 38 | return $ukey; 39 | } 40 | else{ 41 | return false; 42 | } 43 | 44 | } catch (Exception $e) { 45 | if(DEBUG_MODE)die($e->getMessage()); 46 | die('Fail to excute command'); 47 | } 48 | } 49 | 50 | // Get the url by short key | 根据短代码获取原链接 51 | function urlSelect($ukey){ 52 | if(ukeyCheck($ukey)){ 53 | global $dbc; 54 | try{ 55 | $stmt = $dbc->prepare("SELECT `url` FROM `".DB_TABLE."` WHERE ukey = :ukey LIMIT 1"); 56 | $stmt->bindParam(':ukey',$ukey); 57 | $stmt->execute(); 58 | } catch (Exception $e) { 59 | if(DEBUG_MODE)die($e->getMessage()); 60 | } 61 | $result=$stmt->fetch(PDO::FETCH_ASSOC); 62 | if(!empty(trim($result['url'])))return $result['url']; 63 | else return null; 64 | } 65 | 66 | } 67 | 68 | // Url short key generate | 短链接代码生成(去掉了容易混淆的字母) 69 | function ukeyGen(){ 70 | $s='23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'; 71 | $sLength=strlen($s)-1; 72 | $length=rand(MIN_UKEY_LENGTH,MAX_UKEY_LENGTH); 73 | $result=''; 74 | for($i=0;$i<$length;$i++){ 75 | $result.=$s[rand(0,$sLength)]; 76 | } 77 | return $result; 78 | } 79 | 80 | // Check the Url short key exist | 短链接代码是否存在 81 | function ukeyCheck($ukey){ 82 | global $dbc; 83 | try{ 84 | $stmt = $dbc->prepare("SELECT `ukey` FROM `".DB_TABLE."` WHERE ukey = :ukey LIMIT 1"); 85 | $stmt->bindParam(':ukey',$ukey); 86 | $stmt->execute(); 87 | 88 | if($result=$stmt->fetch(PDO::FETCH_ASSOC)){ 89 | return true; 90 | } else { 91 | return false; 92 | } 93 | 94 | } catch (Exception $e) { 95 | if(DEBUG_MODE)die($e->getMessage()); 96 | } 97 | } 98 | 99 | //404 Page | 404页面 100 | function notfound(){ 101 | die("
"); 102 | } 103 | 104 | //Json Render | Json格式化返回 105 | function jsonRender($content){ 106 | header("Content-Type:application/json; charset=UTF-8"); 107 | echo json_encode($content); 108 | die(); 109 | } 110 | 111 | -------------------------------------------------------------------------------- /page.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | <?php echo SITE_NAME; ?> 7 | 8 | 9 | 10 | 32 | 33 | 34 |
35 |
36 |

37 | 38 |

39 |

原始链接

40 | 41 | 42 |

短链接

43 | 44 | 45 | 46 |
47 |
48 | 52 | 85 | 86 | 87 | --------------------------------------------------------------------------------