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:
-
- 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 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:
- RewriteEngine On
- RewriteBase/
- RewriteRule ^/(. *) $ link. php? Url = $1 [L]