Php code for generating short URLs

Source: Internet
Author: User
Tags chr php code

Generate a short URL program

The code is as follows: Copy code

<? Php
Function code62 ($ 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;
}
Function compute url ($ url ){
$ Url = crc32 ($ url );
$ Result = sprintf ("% u", $ url );
Return code62 ($ result );
}

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

The code is as follows: Copy code

How to set

Http://z.cn/link.php? Url = http://www.111cn.net

Scale down

Http://z.cn/zHEYrvV

In this case, url rewriting is required. In this example, you can rewrite it as follows:

The code is as follows: Copy code

RewriteEngine On

RewriteBase/

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.