php实现类似微博短链接功能

/ 1评 / 3

闲来无事,自己用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 $url;
    //return $result;
    return code62($result);
}
 
echo shorturl("https://www.fengxiaopeng.cn");
?>

上述算法函数会将每个网址生成唯一一个对应码,如果提交的网址已经生成过(即在数据库中存在),那么会将生成次数加一(即带统计网址生成次数)。半完整代码已上传至github和码云,传送门--github:https://github.com/DoubMonkey/sinashortlink 码云:https://gitee.com/fengxiaop/sinashortlink

  1. 头像 说道:

    没有重写跳转吗

发表回复

您的电子邮箱地址不会被公开。