To generate a short URL, we can directly use a function to generate a unique address with a length of 5-6 characters, but what we need to do is to directly use it to make a static jump. I will introduce it to you below.
Generate short URL program
The 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 shorturl($url){
$url=crc32($url);
$result=sprintf("%u",$url);
return code62($result);
}
|
| Copy code
|
代码如下 |
复制代码 |
如何将
http://z.cn/link.php?url=http://www.bKjia.c0m
缩成
http://z.cn/zHEYrvV
|
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 shorturl($url){
$url=crc32($url);
$result=sprintf("%u",$url);
return code62($result);
}
代码如下 |
复制代码 |
RewriteEngine On
RewriteBase /
RewriteRule ^/(.*)$ link.php?url=[L]
|
|
The job generated in this way is like htt:/z.cn/abcfc, but if we want to achieve access, we need to configure a pseudo-static in your apache or iis, as follows
The code is as follows
|
Copy code
How to convert
http://z.cn/link.php?url=http://www.bKjia.c0m
Shrunk to
http://z.cn/zHEYrvV
This place needs to use url rewriting. According to this example, it can be rewritten like this:
The code is as follows
|
Copy code
|
|
|
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn