Php code for generating short URLs

Source: Internet
Author: User
To generate a short URL in PHP, we can use a function to generate a unique address with a length of 5-6 characters. However, we also need to use static redirection directly. the specific instance code

To generate a short URL in PHP, we can use a function to generate a unique address with a length of 5-6 characters. However, we also need to use static redirection directly, the specific instance code is as follows:

  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. }

In this way, the generated job is like htt:/z.cn/abcfc, but to achieve access, we need to configure a pseudo static state in your apache or iis.

Http://z.cn/link.php? Url = http://www.phpfensi.com scale into http://z.cn/zHEYrvV

In this case, url rewriting is required. In this example, the code is as follows:

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

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.