[文章目录]显示/隐藏
还是记录
哈哈,
这个功能主要用在我音乐收藏中
变短而且也查看源码码搜索不到mp3字样咯,
毕竟我都是解析网易音乐添加数据库的,
原理
短网址的实现原理就是有一个数据表会配置文件将短网址和实际网址进行对应,
当请求某个短网址时,
程序跳转到对应的实际网址上去,
从而实现网址的访问.
生成短网址
- 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);
- }
使用以上PHP代码可以生成唯一的6位的短网址,
然后我们将生成的短网址与原网址一起写入到MySQL表中,
插入数据库的代码这里我就不写了,
这是PHP基础.
建立url.php跳转文件
- $url = $_GET['url'];
- if(isset($url) && !emptyempty($url)){
- $sql = "select url from shorturl where codeid='$url'";
- $query = mysql_query($sql);
- if($row=mysql_fetch_array($query)){
- $real_url = $row['url'];
- header('Location: ' . $real_url);
- }else{
- header('HTTP/1.0 404 Not Found');
- echo 'Unknown link.';
- }
- }else{
- header('HTTP/1.0 404 Not Found');
- echo 'Unknown link.';
- }
完成
代码中,如果得到短网址对应的真实url,
会使用header跳转到真实的页面上去,
否则返回404代码.
这样我们可以使用如:http://yourdomain/url.php?url=xxx来实现短网址访问.
I'm really enjoying the theme/design of your blog.
Do you ever run into any web browser compatibility problems?
A number of my blog readers have complained about my blog not working correctly in Explorer but looks
great in Chrome. Do you have any recommendations to help fix this issue?
@dying light pc:This can only be adjusted slowly.
如果把插入数据库这一步也写出来那教程就完美了~
@龙笑天: 呃, 现在解析,都是有时间限制的,数据库没啥用了