sinaurl.cn新浪短网址 绿标免费生成原理介绍

介绍一个免费生成https://sinaurl.cn/xxx 的网址:http://www.sinaurls.cn/  顺便写一个短网址的php代码原理。

核心方法  代码php:

functionmake_short_url($url){

    $url=str_ireplace("http://","",$url);

    $pdo=newPDO("mysql:host=".HOST.";dbname=".DB_NAME,USER,PASS);

    $rs=$pdo->query("select id from links where url='".$url."'");

    $row=$rs-> fetch();

    if($row==false){

        $pdo->exec("insert into links(url,ctime) values('".$url."','".mktime()."')");

        $id=$pdo-> lastinsertid();

        return"http://d.cn/".decb64($id);

    }else{

        return"http://d.cn/".decb64($row['id']);

    }

}

functionget_long_url($code){

    $pdo=newPDO("mysql:host=".HOST.";dbname=".DB_NAME,USER,PASS);

    $rs=$pdo->query("select url from links where id='".b64dec($code)."'");

    $row=$rs-> fetch();

    if($row==false){

        print"链接错误";

        exit;

    }else{

        return"http://".$row['url'];

    }

}//参数的接收与短链接返回部分if($_GET['code']){

    $code=trim($_GET['code'],"/");

    $url=get_long_url($code);

    if($url){

        header("location:$url");

    }

}elseif($_GET['url']){

    $url=trim($_GET['url']);

    printmake_short_url($url);

}

最后浏览器测试下

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容