php實現短鏈接系統

短鏈接的好處
節省網址長度,便於社交化傳播。(短信或者微博,字數長度受限制時。這樣的短鏈接製作二維碼也是易於識別)
方便後臺跟蹤點擊量、地域分佈等用戶統計。(統計流量訪問,延伸可以做網址舉報屏蔽。只屏蔽短鏈接即可,控制輸出)
規避關鍵詞、域名屏蔽手段。
隱藏真實地址,適合做付費推廣鏈接。

實現思路:

將傳入的長鏈接存入數據庫中,並記錄創建時間,返回自增ID。

將返回的id進行10進制轉64進制編碼:如自增id爲10時,64進制轉換爲A;即使當我們記錄到1億條時,1億的64進製爲:F9eEA,才5個字符長度,非常適合作爲短鏈接的參數。
然後組合成 http://域名/編碼參數/。我們既得到了短鏈接。當我們訪問短鏈接時,解析參數轉爲10進制。到數據庫中查找獲得相應的鏈接,進行301跳轉。此功能既爲完成。


步驟如下:

1、修改host文件->配置網站虛擬目錄->進行URL重寫
1.1 修改hosts文件,將測試域名指向本機ip
[html] view plain copy
  1. C:\Windows\System32\drivers\etc\hosts  
  2. 127.0.0.1            d.cn   

1.2 配置apache,添加虛擬域名映射,參考:http://blog.csdn.net/nuli888/article/details/51830659
[html] view plain copy
  1. <VirtualHost *:80>  
  2.     DocumentRoot "D:/webroot/test/link"  
  3.     ServerName d.cn  
  4.     <Directory "D:/webroot/test/link">  
  5.         DirectoryIndex index.html index.php   
  6.         AllowOverride All  
  7.         Order deny,allow  
  8.         Allow from all  
  9.     </Directory>  
  10. </VirtualHost>  
  11.   
  12.   
  13. <VirtualHost *:80>  
  14.     DocumentRoot "D:/webroot"  
  15.     ServerName localhost  
  16.     <Directory "D:/webroot">  
  17.         DirectoryIndex index.html index.PHP   
  18.         AllowOverride All  
  19.         Order deny,allow  
  20.         Allow from all  
  21.     </Directory>  
  22. </VirtualHost>  

1.3 配置.htaccess文件,將短連接重定向,設置如下:

[html] view plain copy
  1. <IfModule mod_rewrite.c>  
  2. RewriteEngine on  
  3. RewriteRule ^(\S{1,7})$ index.php?code=$1 [L]  
  4. </IfModule>  

URL短鏈接的實現原理和方法

2、增加64進制編碼與解碼方法:

func.PHP

[html] view plain copy
  1. <?php  
  2. function b64dec($b64) { //64進制轉換成10進制  
  3.     $map = array(  
  4.         '0'=>0,'1'=>1,'2'=>2,'3'=>3,'4'=>4,'5'=>5,'6'=>6,'7'=>7,'8'=>8,'9'=>9,  
  5.         'A'=>10,'B'=>11,'C'=>12,'D'=>13,'E'=>14,'F'=>15,'G'=>16,'H'=>17,'I'=>18,'J'=>19,  
  6.         'K'=>20,'L'=>21,'M'=>22,'N'=>23,'O'=>24,'P'=>25,'Q'=>26,'R'=>27,'S'=>28,'T'=>29,  
  7.         'U'=>30,'V'=>31,'W'=>32,'X'=>33,'Y'=>34,'Z'=>35,'a'=>36,'b'=>37,'c'=>38,'d'=>39,  
  8.         'e'=>40,'f'=>41,'g'=>42,'h'=>43,'i'=>44,'j'=>45,'k'=>46,'l'=>47,'m'=>48,'n'=>49,  
  9.         'o'=>50,'p'=>51,'q'=>52,'r'=>53,'s'=>54,'t'=>55,'u'=>56,'v'=>57,'w'=>58,'x'=>59,  
  10.         'y'=>60,'z'=>61,'_'=>62,'='=>63  
  11.     );  
  12.     $dec = 0;  
  13.     $len = strlen($b64);  
  14.     for ($i = 0; $i < $len; $i++) {  
  15.         $b = $map[$b64{$i}];  
  16.         if ($b === NULL) {  
  17.             return FALSE;  
  18.         }  
  19.         $j = $len - $i - 1;  
  20.         $dec += ($j == 0 ? $b : (2 << (6 * $j - 1)) * $b);  
  21.     }  
  22.     return $dec;  
  23. }  
  24. function decb64($dec) { //10進制轉換成64進制  
  25.     if ($dec < 0) {  
  26.         return FALSE;  
  27.     }  
  28.     $map = array(  
  29.         0=>'0',1=>'1',2=>'2',3=>'3',4=>'4',5=>'5',6=>'6',7=>'7',8=>'8',9=>'9',  
  30.         10=>'A',11=>'B',12=>'C',13=>'D',14=>'E',15=>'F',16=>'G',17=>'H',18=>'I',19=>'J',  
  31.         20=>'K',21=>'L',22=>'M',23=>'N',24=>'O',25=>'P',26=>'Q',27=>'R',28=>'S',29=>'T',  
  32.         30=>'U',31=>'V',32=>'W',33=>'X',34=>'Y',35=>'Z',36=>'a',37=>'b',38=>'c',39=>'d',  
  33.         40=>'e',41=>'f',42=>'g',43=>'h',44=>'i',45=>'j',46=>'k',47=>'l',48=>'m',49=>'n',  
  34.         50=>'o',51=>'p',52=>'q',53=>'r',54=>'s',55=>'t',56=>'u',57=>'v',58=>'w',59=>'x',  
  35.         60=>'y',61=>'z',62=>'_',63=>'=',  
  36.     );  
  37.     $b64 = '';  
  38.     do {  
  39.         $b64 = $map[($dec % 64)] . $b64;  
  40.         $dec /= 64;  
  41.     } while ($dec >= 1);  
  42.     return $b64;  
  43.       
  44. }  


3、創建數據表links

[html] view plain copy
  1. CREATE TABLE `links` (  
  2.   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,  
  3.   `url` varchar(255) DEFAULT NULL,  
  4.   `ctime` int(11) DEFAULT NULL,  
  5.   PRIMARY KEY (`id`)  
  6. ENGINE=MyISAM AUTO_INCREMENT=26262628 DEFAULT CHARSET=latin1;  
  7.   
  8.   
  9. -- ----------------------------  
  10. -- Records of links  
  11. -- ----------------------------  
  12. INSERT INTO `links` VALUES ('10900', 'http://www.cnblogs.com', '1489568387');  
  13. INSERT INTO `links` VALUES ('10901', 'http://www.baidu.com', '1489569782');  


4、創建短鏈接生成與長鏈接獲取功能

index.php

[html] view plain copy
  1. <?php  
  2. include 'func.php';  
  3. define("HOST","localhost");  
  4. define("DB_NAME","test");  
  5. define("USER","root");  
  6. define("PASS","123456");  
  7.   
  8. function make_short_url($url){  
  9.     $url=str_ireplace("http://","",$url);  
  10.     $pdo = new PDO("mysql:host=".HOST.";dbname=".DB_NAME,USER,PASS);   
  11.     $rs = $pdo ->query("select id from links where url='".$url."'");  
  12.     $row = $rs -> fetch();   
  13.     if($row==false){  
  14.         $pdo -> exec("insert into links(url,ctime) values('".$url."','".mktime()."')");   
  15.         $id=$pdo -> lastinsertid();   
  16.         return "http://d.cn/".decb64($id);  
  17.     }else{  
  18.         return "http://d.cn/".decb64($row['id']);  
  19.     }  
  20. }  
  21.   
  22. function get_long_url($code){  
  23.     $pdo = new PDO("mysql:host=".HOST.";dbname=".DB_NAME,USER,PASS);   
  24.     $rs = $pdo ->query("select url from links where id='".b64dec($code)."'");  
  25.     $row = $rs -> fetch();   
  26.     if($row==false){  
  27.         print "鏈接錯誤";  
  28.         exit;  
  29.     }else{  
  30.         return "http://".$row['url'];  
  31.     }  
  32. }  
  33. //參數的接收與短鏈接返回部分  
  34. if($_GET['code']){  
  35.     $code=trim($_GET['code'],"/");  
  36.     $url=get_long_url($code);  
  37.     if($url){  
  38.         header("location:$url");  
  39.     }  
  40. }elseif($_GET['url']){  
  41.     $url=trim($_GET['url']);  
  42.     print make_short_url($url);  
  43. }  

5、測試



返回內容爲:http://d.cn/2gL。在瀏覽器中訪問http://d.cn/2gL,即會跳轉到http://www.baidu.com

一個短鏈接已經基本實現,仍然有一些可待優化的地方。如跳轉方式改爲301跳轉;數據庫數據量大時,可以採取分庫操作。或者用memcache或者Redis緩存服務器來代替MySQL,提升效率等等

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章