Ideas and implementation of generating short URLs in php

Source: Internet
Author: User
Ideas and implementation of generating short URLs in php

  1. RewriteEngine On
  2. RewriteBase/
  3. RewriteRule ^/(. *) $ link. php? Url = $1 [L]

Implementing the http://t.cn/link.php? Url = zHEYrvV conversion for http://t.cn/zHEYrvV, shorten a lot, then how to through zHEYrvV to find the http://bbs.it-home.org/sitejs-17300-1.html this url and jump to this url? Here we use an encryption-like algorithm to shorten all the long URLs into a corresponding string of 5-6 characters, and store the corresponding relationship in the database.

In combination with this example, you can find the corresponding URL in the database based on the input parameter zHEYrvV. if you find the URL, the header will jump over.

Share the code for generating a php short URL (the long URL can be generated as short as 5-6 characters and must be unique ):

  1. Function code62 ($ x ){
  2. $ Show = '';
  3. While ($ x> 0 ){
  4. $ S = $ x % 62;
  5. If ($ s> 35 ){
  6. $ S = chr ($ s + 61 );
  7. } Elseif ($ s> 9 & $ s <= 35 ){
  8. $ S = chr ($ s + 55 );
  9. }
  10. $ Show. = $ s;
  11. $ X = floor ($ x/62 );
  12. }
  13. Return $ show;
  14. }
  15. Function compute url ($ url ){
  16. $ Url = crc32 ($ url );
  17. $ Result = sprintf ("% u", $ url );
  18. Return code62 ($ result );
  19. }

For example, echo response url ('http: // bbs.it-home.org/'); will generate a unique correspondence code.

For url rewriting methods, refer to: php pseudo-static url rewriting simple example php pseudo-static (url rewriting) writing method for quick url rewriting in php5.3 or a later version.

Related Article

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.