Ideas and implementation of generating short URLs in php
- RewriteEngine On
- RewriteBase/
- 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 ):
- 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 );
- }
-
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. |