The idea and method of generating short URLs in PHP (recommended)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.