├── .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 |