简单的生成短网址

WAMP环境下

1、首先是post.html页面,用ajax请求返回短网址

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $("button").click(function(){
            var text = $("#text").val();
            $.ajax({
                url     : "shortUrl.php",
                type    : 'post',
                data    : {'text':text},
                success : function(data){
                  $("#url").val(data);
                }
            })
        });
    })
</script>
<body>

    长网址: <input type="text" name="url" id="text" size="50">

    <button type="button" >提交</button><br/>
    短网址: <input type="text" id="url" val="" size="50">

</body>
</html>


2、shortUrl.php页面,把原网址和随机生成的8位码存入数据库,形成关联

#接受post.html页面提交过来的数据
$url = $_POST['text'];//post.html提交过来的url

#生成随机8位码
$txt = 'Z1X2C3V4B5N6M7A8S9D0FGHJKLQWERTYUIOP';
$code = '';
for ($i = 0; $i < 8; $i++) {
    $code .= $txt[mt_rand(0, 35)];
}

#把前端提交过来的原网址($url)和随机生成的8位码($str)一起存入数据库,目的为了到时候取的时候关联获取原网址
$con = new mysqli('localhost','root','root','shorturl');
$sql = "insert into `short_url` (url,code) VALUES ('$url','$code')";//插入数据库
$result = mysqli_query($con,$sql);

#接下来拼接成短网址,然后返回前端页面
第一步:这是没有处理的网址如下(方便理解),后面进行伪静态和虚拟主机配置,就可以很简短
'localhost/shorturl/getUrl.php'这个是当输入短网址后,就可以到getUrl.php页面进行处理并重定向操作

echo 'http://localhost/shorturl/getUrl.php/'.$code;//这里是还没有处理的短网址,拼接$code目的是后面从浏览器地址获取进行数据库匹配

第二步:接下来就是如何把上面的短网址处理成自己想要的真正短网址
1.找到你的apache下面 conf/httpd.conf文件,把LoadModule rewrite_module modules/mod_rewrite.so 前面'#'去掉,表示启用重写功能,

并且也要去掉 Include conf/extra/httpd-vhosts.conf 前面 '#',表示启用虚拟主机配置

2、在你的文件根目录(也就是wamp/www/文件夹/)下,新建文件 .htaccess (注意文件名前面有 . )

<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ getUrl.php/$1 [QSA,PT,L]
</IfModule>

RewriteRule ^(.*)$ getUrl.php/$1 [QSA,PT,L] 中 getUrl.php 表示要隐藏的文件名

第三步:在apache下的 conf/extra/httpd-vhosts.conf 加入
<VirtualHost *:80>
DocumentRoot "D:/wamp/www/shorturl/"
    ServerName sgw.me
</VirtualHost>
DocumentRoot 你的文件目录,ServerName 你的短网址(随意),配置好要重启apache才能生效

第四步:把你的 C:\Windows\System32\drivers\etc\hosts  文件添加(要和你上面的ServerName保持一致,apache才能读取到)
127.0.0.1       sgw.me
现在可以把上面“第一步”的 echo 'http://localhost/shorturl/getUrl.php/'.$code 换成
echo 'http://sgw.me/'.$code;//这个就是真正短网址,例如:http://sgw.me/WNL740SY 


3、getUrl.php页面,当前端获取生成的短网址之后,这里获取短网址附带的8位随机码进行数据库查询原网址,然后重定向

# 获取短网址的后8位随机码
$get_url = '';
if (isset($_SERVER['PATH_INFO'])) {
    $get_url = $_SERVER['PATH_INFO'];
}else if (isset($_SERVER['REDIRECT_PATH_INFO'])) {
    $get_url = $_SERVER['REDIRECT_PATH_INFO'];
}else if (isset($_SERVER['REDIRECT_URL'])) {
    $get_url = $_SERVER['REDIRECT_URL'];
}
#处理字符,因含有 '/'
$get_url = ltrim($get_url,'/');//获得url地址栏额外参数

#得到额外参数,进行判断
if(isset($get_url) && !empty($get_url)){//针对 0 null '' 都是empty
    $con = new mysqli('localhost','root','root','shorturl');
    $sql = "select url from `short_url` where code='$get_url'";
    $result = mysqli_query($con,$sql);

    #关联数组(重定向)
    if($row=mysqli_fetch_assoc($result)) {
        $real_url = $row['url'];
        header('Location: ' . $real_url);//这里根据8位随机码对应取出原网址,就是重定向
    }else {
        header('HTTP/1.0 404 Not Found');
        echo 'Unknown link.';
    }

}else{
    header('HTTP/1.0 404 Not Found');
    echo 'Unknown link.';
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值