您的位置:首页 > 编程语言 > PHP开发

php实现短链接系统

2017-07-31 16:58 309 查看
短链接的好处:
节省网址长度,便于社交化传播。(短信或者微博,字数长度受限制时。这样的短链接制作二维码也是易于识别)
方便后台跟踪点击量、地域分布等用户统计。(统计流量访问,延伸可以做网址举报屏蔽。只屏蔽短链接即可,控制输出)
规避关键词、域名屏蔽手段。
隐藏真实地址,适合做付费推广链接。

实现思路:

将传入的长链接存入数据库中,并记录创建时间,返回自增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

C:\Windows\System32\drivers\etc\hosts  

127.0.0.1            d.cn   

1.2 配置apache,添加虚拟域名映射,参考:http://blog.csdn.net/nuli888/article/details/51830659

[html] view
plain copy

<VirtualHost *:80>  

    DocumentRoot "D:/webroot/test/link"  

    ServerName d.cn  

    <Directory "D:/webroot/test/link">  

        DirectoryIndex index.html index.php   

        AllowOverride All  

        Order deny,allow  

        Allow from all  

    </Directory>  

</VirtualHost>  

  

  

<VirtualHost *:80>  

    DocumentRoot "D:/webroot"  

    ServerName localhost  

    <Directory "D:/webroot">  

        DirectoryIndex index.html index.PHP   

        AllowOverride All  

        Order deny,allow  

        Allow from all  

    </Directory>  

</VirtualHost>  

1.3 配置.htaccess文件,将短连接重定向,设置如下:

[html] view
plain copy

<IfModule mod_rewrite.c>  

RewriteEngine on  

RewriteRule ^(\S{1,7})$ index.php?code=$1 [L]  

</IfModule>  

URL短链接的实现原理和方法

2、增加64进制编码与解码方法:

func.PHP

[html] view
plain copy

<?php  

function b64dec($b64) { //64进制转换成10进制  

    $map = array(  

        '0'=>0,'1'=>1,'2'=>2,'3'=>3,'4'=>4,'5'=>5,'6'=>6,'7'=>7,'8'=>8,'9'=>9,  

        'A'=>10,'B'=>11,'C'=>12,'D'=>13,'E'=>14,'F'=>15,'G'=>16,'H'=>17,'I'=>18,'J'=>19,  

        'K'=>20,'L'=>21,'M'=>22,'N'=>23,'O'=>24,'P'=>25,'Q'=>26,'R'=>27,'S'=>28,'T'=>29,  

        'U'=>30,'V'=>31,'W'=>32,'X'=>33,'Y'=>34,'Z'=>35,'a'=>36,'b'=>37,'c'=>38,'d'=>39,  

        'e'=>40,'f'=>41,'g'=>42,'h'=>43,'i'=>44,'j'=>45,'k'=>46,'l'=>47,'m'=>48,'n'=>49,  

        'o'=>50,'p'=>51,'q'=>52,'r'=>53,'s'=>54,'t'=>55,'u'=>56,'v'=>57,'w'=>58,'x'=>59,  

        'y'=>60,'z'=>61,'_'=>62,'='=>63  

    );  

    $dec = 0;  

    $len = strlen($b64);  

    for ($i = 0; $i < $len; $i++) {  

        $b = $map[$b64{$i}];  

        if ($b === NULL) {  

            return FALSE;  

        }  

        $j = $len - $i - 1;  

        $dec += ($j == 0 ? $b : (2 << (6 * $j - 1)) * $b);  

    }  

    return $dec;  

}  

function decb64($dec) { //10进制转换成64进制  

    if ($dec < 0) {  

        return FALSE;  

    }  

    $map = array(  

        0=>'0',1=>'1',2=>'2',3=>'3',4=>'4',5=>'5',6=>'6',7=>'7',8=>'8',9=>'9',  

        10=>'A',11=>'B',12=>'C',13=>'D',14=>'E',15=>'F',16=>'G',17=>'H',18=>'I',19=>'J',  

        20=>'K',21=>'L',22=>'M',23=>'N',24=>'O',25=>'P',26=>'Q',27=>'R',28=>'S',29=>'T',  

        30=>'U',31=>'V',32=>'W',33=>'X',34=>'Y',35=>'Z',36=>'a',37=>'b',38=>'c',39=>'d',  

        40=>'e',41=>'f',42=>'g',43=>'h',44=>'i',45=>'j',46=>'k',47=>'l',48=>'m',49=>'n',  

        50=>'o',51=>'p',52=>'q',53=>'r',54=>'s',55=>'t',56=>'u',57=>'v',58=>'w',59=>'x',  

        60=>'y',61=>'z',62=>'_',63=>'=',  

    );  

    $b64 = '';  

    do {  

        $b64 = $map[($dec % 64)] . $b64;  

        $dec /= 64;  

    } while ($dec >= 1);  

    return $b64;  

      

}  

3、创建数据表links

[html] view
plain copy

CREATE TABLE `links` (  

  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,  

  `url` varchar(255) DEFAULT NULL,  

  `ctime` int(11) DEFAULT NULL,  

  PRIMARY KEY (`id`)  

) ENGINE=MyISAM AUTO_INCREMENT=26262628 DEFAULT CHARSET=latin1;  

  

  

-- ----------------------------  

-- Records of links  

-- ----------------------------  

INSERT INTO `links` VALUES ('10900', 'http://www.cnblogs.com', '1489568387');  

INSERT INTO `links` VALUES ('10901', 'http://www.baidu.com', '1489569782');  

4、创建短链接生成与长链接获取功能

index.php

[html] view
plain copy

<?php  

include 'func.php';  

define("HOST","localhost");  

define("DB_NAME","test");  

define("USER","root");  

define("PASS","123456");  

  

function make_short_url($url){  

    $url=str_ireplace("http://","",$url);  

    $pdo = new PDO("mysql:host=".HOST.";dbname=".DB_NAME,USER,PASS);   

    $rs = $pdo ->query("select id from links where url='".$url."'");  

    $row = $rs -> fetch();   

    if($row==false){  

        $pdo -> exec("insert into links(url,ctime) values('".$url."','".mktime()."')");   

        $id=$pdo -> lastinsertid();   

        return "http://d.cn/".decb64($id);  

    }else{  

        return "http://d.cn/".decb64($row['id']);  

    }  

}  

  

function get_long_url($code){  

    $pdo = new PDO("mysql:host=".HOST.";dbname=".DB_NAME,USER,PASS);   

    $rs = $pdo ->query("select url from links where id='".b64dec($code)."'");  

    $row = $rs -> fetch();   

    if($row==false){  

        print "链接错误";  

        exit;  

    }else{  

        return "http://".$row['url'];  

    }  

}  

//参数的接收与短链接返回部分  

if($_GET['code']){  

    $code=trim($_GET['code'],"/");  

    $url=get_long_url($code);  

    if($url){  

        header("location:$url");  

    }  

}elseif($_GET['url']){  

    $url=trim($_GET['url']);  

    print make_short_url($url);  

}  

5、测试



返回内容为:http://d.cn/2gL。在浏览器中访问http://d.cn/2gL,即会跳转到http://www.baidu.com

一个短链接已经基本实现,仍然有一些可待优化的地方。如跳转方式改为301跳转;数据库数据量大时,可以采取分库操作。或者用memcache或者Redis缓存服务器来代替MySQL,提升效率等等
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 短链接