├── .htaccess ├── README.md ├── config.php ├── favicon.ico ├── functions.php ├── index.php ├── page.html ├── redirect.php ├── shorten.php └── url.sql /.htaccess: -------------------------------------------------------------------------------- 1 | RewriteEngine On 2 | RewriteBase / 3 | RewriteCond %{REQUEST_FILENAME} !-f 4 | RewriteCond %{REQUEST_FILENAME} !-d 5 | RewriteRule . /index.php [L] 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # English Version: 2 | 3 | # URL-Shorter 4 | 5 | A modern, safe and simple PHP url shorter. 6 | 7 | ## Demo: 8 | [http://bbr.buzz/](http://bbr.buzz/) 9 | 10 | ## Function: 11 | + Use PDO and it's Bindparam to prevent SQL Inject. 12 | + Use Ajax to get the short Code 13 | + Use pure JS to copy the short link 14 | + No more magnificent functions 15 | 16 | ## Installation 17 | 1. Git clone or Download the code and unzip 18 | 2. Add the website in your web server config (Require Apache or Nginx rewrite rules(Similar as Wordpress rewrite rules)) 19 | 3. Import the url.sql to your db 20 | 4. Configure the config.php 21 | 5. Finished 22 | 23 | ## Future: 24 | + Redis cache 25 | + Redis control the rate 26 | 27 | --- 28 | 29 | # 中文简介: 30 | 31 | # 短链接 32 | 33 | 一个现代、安全且简洁的PHP短链接 (PHP是世界上最好F#fIO#@($)#U) 34 | 35 | ## Demo: 36 | [http://bbr.buzz/](http://bbr.buzz/) 37 | 38 | ## 功能: 39 | + 使用PDO和Bindparam防注入 40 | + 使用Ajax获取短链接(免刷新) 41 | + 使用纯JS复制生成的短链接 42 | + 没有更多功能了 43 | 44 | ## 安装方法: 45 | 1. git clone或者下载代码到网站根目录 46 | 2. 配置好伪静态(Apache直接使用根目录下的.htaccess就可以了,Nginx请自行配置(类似Wordpress的规则)) 47 | 3. 导入url.sql到数据库 48 | 4. 配置好配置文件 49 | 5. 完成 50 | 51 | ## 计划中要增加的功能 (计划中,计划中) 52 | + 使用Redis缓存,更快,抗压能力更强 53 | + 使用Redis控制访问速率,抗CC 54 | -------------------------------------------------------------------------------- /config.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 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /url.sql: -------------------------------------------------------------------------------- 1 | -- MySQL dump 10.13 Distrib 5.6.36, for linux-glibc2.5 (x86_64) 2 | -- 3 | -- Host: 10.0.1.3 Database: url 4 | -- ------------------------------------------------------ 5 | -- Server version 5.6.38-log 6 | 7 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 8 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; 9 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; 10 | /*!40101 SET NAMES utf8mb4 */; 11 | /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; 12 | /*!40103 SET TIME_ZONE='+00:00' */; 13 | /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; 14 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 15 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 16 | /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; 17 | 18 | -- 19 | -- Table structure for table `url` 20 | -- 21 | 22 | DROP TABLE IF EXISTS `url`; 23 | /*!40101 SET @saved_cs_client = @@character_set_client */; 24 | /*!40101 SET character_set_client = utf8 */; 25 | CREATE TABLE `url` ( 26 | `id` int(9) NOT NULL AUTO_INCREMENT, 27 | `domain` varchar(30) NOT NULL, 28 | `url` varchar(255) NOT NULL, 29 | `ukey` varchar(12) NOT NULL, 30 | `ip` varchar(15) NOT NULL, 31 | `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 32 | PRIMARY KEY (`id`), 33 | UNIQUE KEY `ukey_2` (`ukey`), 34 | KEY `ukey` (`ukey`) 35 | ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4; 36 | /*!40101 SET character_set_client = @saved_cs_client */; 37 | 38 | -- 39 | -- Dumping data for table `url` 40 | -- 41 | 42 | LOCK TABLES `url` WRITE; 43 | /*!40000 ALTER TABLE `url` DISABLE KEYS */; 44 | /*!40000 ALTER TABLE `url` ENABLE KEYS */; 45 | UNLOCK TABLES; 46 | /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; 47 | 48 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; 49 | /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; 50 | /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; 51 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 52 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; 53 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 54 | /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; 55 | 56 | -- Dump completed on 2018-04-02 21:23:03 57 | --------------------------------------------------------------------------------