首页 >php教程 >php手册 >短网址探究,网址探究

短网址探究,网址探究

WBOY
WBOY原创
2016-06-13 09:12:571518浏览

短网址探究,网址探究

引言

  短网址司空见惯,比如说下面这些

  http://dwz.cn/CSW6Y => http://www.cnblogs.com/iforever/p/4313704.html,

  http://dwz.cn/CSWuP => http://www.cnblogs.com/iforever/p/4279006.html ,等等

  访问这些网址的时候,前后一对的指向同一个页面,我这里只是在举例子,这些短网址在weibo或者类似的社交网络中经常见到,由于原始链接地址特别长,这个时候短网址就会大显威力,简短好记,但也有他的缺点,比如说短网址不一定直接看出网址中的一些信息(在长网址中是可以看到一些信息的)。

  整天接触这个,以至于在还没有真正弄明白他的原理的时候就在思想中烙下了“没什么”的烙印,这就错过了一些知识,这种情况在心理学上应该叫心里暗示吧,有人知道是这样吗?

原理

  首先我猜测这个是通过重定向实现的,dwz.cn是一个服务器,用来接收一些短网址,这些短网址是经过dwz.cn处理之后的,在dwz.cn中有该短网址对应的长网址的记录,然后从数据库中获取短网址对应的长网址,然后302重定向。看看具体是不是这样的。

  通过浏览器访问http://dwz.cn/CSW6Y,对请求进行分析(这里使用chrome的调试工具,也可以使用wireshark之类的工具),可以得到下面的数据:

name: abit version: 1 handle: - rewrite: if (!-d && !-f && path ~ "/(.*)$") goto "do.php?url=$1&act=out"

  handle:后面的就是rewrite规则,新浪云的具体的详细规则到这里看http://sae.sina.com.cn/doc/php/runtime.html#php-app-config,很简单,nginx、apache的配置也类似。

  重定向完成之后还有一个要注意的地方就是在传递参数的时候要进行urlencode,重定向之前要用urldecode。在未使用urlencode的时候传递url类型的参数,在获取的时候可能丢掉部分信息,因此在生成短链接之前传递参数的时候要将url进行escape处理,将特殊字符串进行编码,在访问短链接处理的时候需要对编码的url进行urldecode处理,还原成正常的链接,否则在header跳转的时候不会把该链接当成一个正常的url,跳转之后会把该url附加的之前页面的主机后面类似http://abit.sinaapp.com/www.cnblogs.com,可能会出现错误,因此,这里要特别注意。

  sae的在重定向的时候会有一个问题,会将双反斜线自动过滤为一个,例如从http://abit.sinaapp.com/到http:/abit.sinaapp.com/,注意,这里少了个反斜线,这在处理的时候要特别注意,不然可能会遇到不必要的麻烦。

编码

  主要的处理部分

<?<span>php
</span><span>class</span><span> snapshotUrl{
    </span><span>//</span><span>进行编码的数据库,没6位二进制数对应一个字符,一共需要64位,因此选取
    //52+10+2个特殊字符</span>
    <span>private</span> <span>static</span> <span>$basedb</span> = <span>array</span><span>(
        </span>'(',')','a','b','c','d',
        'e','f','g','h','i','j',
        'k','l','m','n','o','p',
        'q','r','s','t','u','v',
        'w','x','y','z','A','B',
        'C','D','E','F','G','H',
        'I','J','K','L','M','N',
        'O','P','Q','R','S','T',
        'U','V','W','X','Y','Z',
        '0','1','2','3','4','5',
        '6','7','8','9',<span>
    );

    </span><span>private</span> <span>function</span> long2short(<span>$url</span><span>){
        </span><span>$hex</span> = <span>md5</span>(<span>$url</span><span>);
        </span><span>$out</span> = ''<span>;
        </span><span>$hex</span> = 0x7FFFFFFF & (1 * ('0x'.<span>substr</span>(<span>$hex</span>, 0, 8<span>)));
        </span><span>for</span>(<span>$i</span>=0; <span>$i</span><5; <span>$i</span>++<span>){
            </span><span>$index</span> = 0x3f & <span>$hex</span><span>;
            </span><span>$out</span> .= self::<span>$basedb</span>[<span>$index</span><span>];
            </span><span>$hex</span> = <span>$hex</span>>>6<span>;
        }
        </span><span>return</span> <span>$out</span><span>;
    }

    </span><span>public</span> <span>function</span> retJson(<span>$arr</span><span>){
        </span><span>return</span> json_encode(<span>$arr</span><span>);
    }

    </span><span>//</span><span>对url进行映射保存</span>
    <span>public</span> <span>function</span> dispose(<span>$url</span>, <span>$act</span><span>){
        </span><span>$mysql</span> = <span>new</span><span> SaeMysql();
        </span><span>switch</span> (<span>$act</span><span>) {
            </span><span>case</span> 'in':
                <span>$short</span> = <span>$this</span>->long2short(<span>$url</span><span>);
                </span><span>$url</span> = <span>addslashes</span>(<span>$url</span><span>);
                </span><span>$sql</span> = "insert into `tiny_url`(`short`,`long`) values ('{<span>$short</span>}','{<span>$url</span>}')"<span>;
                </span><span>$mysql</span>->runSql(<span>$sql</span><span>);
                </span><span>if</span>(<span>$mysql</span>->errno() != 0<span>){
                    </span><span>echo</span> "生成失败"<span>;
                }</span><span>else</span><span>{
                    </span><span>echo</span> "http://abit.sinaapp.com/{<span>$short</span>}"<span>;
                }
                </span><span>break</span><span>;
            </span><span>case</span> 'out':
                <span>if</span>(<span>strlen</span>(<span>$url</span>) > 5<span>)
                    </span><span>echo</span> <span>$this</span>->retJson(<span>array</span>("code"=>"-1","msg"=>"没有这条记录"<span>));
                </span><span>$sql</span> = "select * from `tiny_url` where `short`='{<span>$url</span>}' limit 1"<span>;
                </span><span>$data</span> = <span>$mysql</span>->getData(<span>$sql</span><span>);
                </span><span>if</span>(!<span>$data</span><span>) {
                    </span><span>echo</span> <span>$this</span>->retJson(<span>array</span>("code"=>"-1","msg"=>"没有这条记录"<span>));
                }</span><span>else</span><span>{
                    </span><span>$location</span> = <span>urldecode</span>(<span>$data</span>[0]['long'<span>]);
                    </span><span>header</span>("Location: {<span>$location</span>}"<span>);
                    </span><span>exit</span><span>();
                }
                </span><span>break</span><span>;
            
            </span><span>default</span>:
                <span>#</span><span> code...</span>
                <span>break</span><span>;
        }
    }    
}

</span><span>$url</span> = <span>isset</span>(<span>$_GET</span>['url']) ? <span>$_GET</span>['url'] : <span>null</span><span>;
</span><span>$act</span> = <span>isset</span>(<span>$_GET</span>['act']) ? <span>$_GET</span>['act'] : <span>null</span><span>;
</span><span>$snapshotUrl</span> = <span>new</span><span> snapshotUrl();
</span><span>if</span>(<span>$url</span> === <span>null</span> || <span>$act</span> === <span>null</span><span>)
    </span><span>echo</span> <span>$snapshotUrl</span>->retJson(<span>array</span>("code"=>"-1","msg"=>"参数错误"<span>));

</span><span>$snapshotUrl</span>->dispose(<span>$url</span>, <span>$act</span>);

 

结果

  我做了个可以测试的小网页:

  http://abit.sinaapp.com/有兴趣的可以试试

 

  本文版权归作者iforever(luluyrt@163.com)所有,未经作者本人同意禁止任何形式的转载,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn