php中生成短网址实现程序代码
PHP生成短网址我们可以直接使用一个函数来生成一个唯一的长度为5-6字符的地址,但是我们还需要做一点就是直接利用为静态做跳转,具体实例代码如下:
<?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 shorturl($url){
$url = crc32($url);
$result = sprintf("%u", $url);
return code62($result);
}
?>这样生成职来的就是如 htt:/z.cn/abcfc 这种了,但是我们要实现能访问就需要在你apache或iis配置一个伪静态了,如何将
http://z.cn/link.php?url=http://www.phprm.com 缩成 http://z.cn/zHEYrvV
这个地方需要用到url重写,按照本例则可以这么重写,代码如下:
RewriteEngine On RewriteBase / RewriteRule ^/(.*)$ link.php?url=$1[L]
永久链接:http://www.phprm.com/develop/fs1104.html
转载随意!带上文章地址吧。