search
Homephp教程php手册php生成短网址的思路与方法(推荐)

生成短网址的思路:如果把短网址还原了,你知道是个什么样子的吗?比如:
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 php 
</span><span function</span> code62(<span $x</span><span ){ 
</span><span $show</span>=''<span ; 
</span><span while</span>(<span $x</span>>0<span ){ 
</span><span $s</span>=<span $x</span> % 62<span ; 
</span><span if</span> (<span $s</span>>35<span ){ 
</span><span $s</span>=<span chr</span>(<span $s</span>+61<span ); 
}</span><span elseif</span>(<span $s</span>>9&&<span $s</span><=35<span ){ 
</span><span $s</span>=<span chr</span>(<span $s</span>+55<span ); 
} 
</span><span $show</span>.=<span $s</span><span ; 
</span><span $x</span>=<span floor</span>(<span $x</span>/62<span ); 
} 
</span><span return</span> <span $show</span><span ; 
} </span><span //</span><span  www.jbxue.com</span>
<span function</span> shorturl(<span $url</span><span ){ 
</span><span $url</span>=<span crc32</span>(<span $url</span><span ); 
</span><span $result</span>=<span sprintf</span>("%u",<span $url</span><span ); 
</span><span return</span> code62(<span $result</span><span ); 
} </span>

echo shorturl('http://www.jbxue.com/');
将生成的一个唯一对应码为 n2Q8e 。

参考文章:

  • php生成短网址示例代码
  • php生成短网址的思路与实现
  • php生成短网址 仿微博短网址生成代码
  • php微博短网址算法 php生成短网址的实现代码
  • php短网址超简单代码
  • php生成短网址的简单代码
  • php 生成短网址的一例代码
  • php短链接、短网址、短url的实现代码
  • php 短网址的实现代码
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function