生成短网址的思路:如果把短网址还原了,你知道是个什么样子的吗?比如:
http://www.jbxue.com/sitejs-17300-1.html
对于以上这个链接,除了直接打开之外,还有一种方法打开它,如下:
http://www. jbxue.com/link.php?url=http://www.jbxue.com/sitejs-17300-1.html
好了,短网址还原了实际就是这个样子的了,可能你看到新浪微博应用里面的短网址都是这个样子:
http://t.cn/zHEYrvV
其实他还原了说不定就是这个样子:
http://t.cn/link.php?url=http://www.jbxue.com/sitejs-17300-1.html
好了,这里就说到第二步了,如何将
http://t.cn/link.php?url=http://www.jbxue.com/sitejs-17300-1.html
缩成
http://t.cn/zHEYrvV
这个地方需要用到url重写,按照本例则可以这么重写:
RewriteEngine On?
RewriteBase /?
RewriteRule ^/(.*)$ link.php?url=$1[L]?
这里就实现了将 http://t.cn/link.php?url=zHEYrvV 转换为了 http://t.cn/zHEYrvV ,缩短了不少,那么如何通过 zHEYrvV 去查找到 http://www.jbxue.com/sitejs-17300-1.html 这个网址并跳到这个网址上去呢?这里就用到了一个类似加密的算法了,通过算法将所有的长网址缩短成一个对应的5-6位的并且唯一字符串,并将这个对应关系存入到数据库中去。结合本例就是根据传入的参数 zHEYrvV 到数据库中去找对应的网址,找到了就 header 跳转过去。
ok,至于生成短网址的思路就是这个样子的了。
通过php生成短网址的那个过程(这里将长网址生成短至5-6位字符长度并且还需要是唯一的):
<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">php </span><span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 1.5 !important;">function</span> code62(<span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$x</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">){ </span><span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$show</span>=''<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">; </span><span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 1.5 !important;">while</span>(<span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$x</span>>0<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">){ </span><span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$s</span>=<span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$x</span> % 62<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">; </span><span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 1.5 !important;">if</span> (<span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$s</span>>35<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">){ </span><span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$s</span>=<span style="margin: 0px; padding: 0px; color: #008080; line-height: 1.5 !important;">chr</span>(<span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$s</span>+61<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">); }</span><span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 1.5 !important;">elseif</span>(<span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$s</span>>9&&<span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$s</span>){ <span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$s</span>=<span style="margin: 0px; padding: 0px; color: #008080; line-height: 1.5 !important;">chr</span>(<span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$s</span>+55<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">); } </span><span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$show</span>.=<span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$s</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">; </span><span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$x</span>=<span style="margin: 0px; padding: 0px; color: #008080; line-height: 1.5 !important;">floor</span>(<span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$x</span>/62<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">); } </span><span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 1.5 !important;">return</span> <span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$show</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">; } </span><span style="margin: 0px; padding: 0px; color: #008000; line-height: 1.5 !important;">//</span><span style="margin: 0px; padding: 0px; color: #008000; line-height: 1.5 !important;"> www.jbxue.com</span><span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 1.5 !important;">function</span> shorturl(<span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$url</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">){ </span><span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$url</span>=<span style="margin: 0px; padding: 0px; color: #008080; line-height: 1.5 !important;">crc32</span>(<span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$url</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">); </span><span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$result</span>=<span style="margin: 0px; padding: 0px; color: #008080; line-height: 1.5 !important;">sprintf</span>("%u",<span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$url</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">); </span><span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 1.5 !important;">return</span> code62(<span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">$result</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">); } </span>
Copier après la connexion
比如
echo shorturl('http://www.jbxue.com/');?
将生成的一个唯一对应码为 n2Q8e 。
参考文章:
- php生成短网址示例代码
- php生成短网址的思路与实现
- php生成短网址 仿微博短网址生成代码
- php微博短网址算法 php生成短网址的实现代码
- php短网址超简单代码
- php生成短网址的简单代码
- php 生成短网址的一例代码
- php短链接、短网址、短url的实现代码
- php 短网址的实现代码