The idea of generating short URLs and the implementation of using PHP to generate short URLs.
The idea of creating a short URL: If you restore a short URL, do you know what it looks like? Like what:
Http://www.jbxue.com/sitejs-17300-1.html
For the above link, in addition to directly open, there is a way to open it, as follows:
http://www. Jbxue.com/link.php?url=http://www.jbxue.com/sitejs-17300-1.html
OK, the short URL to restore the actual is the case, you may see the Sina Weibo app inside the short URL is this look like:
Http://t.cn/zHEYrvV
In fact, he restored it might be this way:
Http://t.cn/link.php?url=http://www.jbxue.com/sitejs-17300-1.html
Well, here's the second step, how to
Http://t.cn/link.php?url=http://www.jbxue.com/sitejs-17300-1.html
Shrink into
Http://t.cn/zHEYrvV
This place requires a URL rewrite, which, in this case, can be rewritten like this:
Rewriteengine on
Rewritebase/
Rewriterule ^/(. *) $ link.php?url=$1[l]
Here to achieve the conversion of Http://t.cn/link.php?url=zHEYrvV to Http://t.cn/zHEYrvV, shortened a lot, then how to zheyrvv to find http://www.jbxue.com/ sitejs-17300-1.html this URL and jump to this URL? Here is a similar encryption algorithm, through the algorithm to shorten all the long URLs into a corresponding 5-6-bit and unique string, and the corresponding relationship into the database. Combined with this example is based on the parameters passed zheyrvv to the database to find the corresponding URL, found the header jump past.
OK, the idea of generating a short URL is this way.
The process of generating a short URL via PHP (here the long URLs are generated as short as 5-6 character lengths and need to be unique):
<?PHPfunctionCode62 ($x){ $show= ' '; while($x>0){ $s=$x% 62; if($s>35){ $s=CHR($s+61); }ElseIf($s>9&&$s<=35){ $s=CHR($s+55); } $show.=$s; $x= Floor($x/62); } return $show; } //www.jbxue.comfunctionShorturl ($url){ $url=CRC32($url); $result=sprintf("%u",$url); returnCode62 ($result); }
Like what
echo shorturl (' http://www.jbxue.com/');
A unique corresponding code that will be generated is n2q8e.
Reference article:
- PHP generates short URL sample code
- The idea and implementation of PHP generating short URLs
- PHP Generate short URL imitation microblogging short URL generation code
- PHP short URL algorithm php generated short URL implementation code
- PHP short URL Super simple code
- Simple code for PHP to generate short URLs
- A sample code for PHP to generate a short URL
- PHP short link, short URL, short URL implementation code
- PHP Short URL Implementation code