1.http://www.snippetit.com/2009/04/php-short-url-algorithm-implementation/

The following code is written according to the algorithm above excluding the database checking part for duplication:

function shorturl($input) {
$base32 = array (
'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'
); $hex = md5($input);
$hexLen = strlen($hex);
$subHexLen = $hexLen / 8;
$output = array(); for ($i = 0; $i < $subHexLen; $i++) {
$subHex = substr ($hex, $i * 8, 8);
$int = 0x3FFFFFFF & (1 * ('0x'.$subHex));
$out = ''; for ($j = 0; $j < 6; $j++) {
$val = 0x0000001F & $int;
$out .= $base32[$val];
$int = $int >> 5;
} $output[] = $out;
} return $output;
}
Sample code to test/use the above function: $input = 'http://www.snippetit.com/1';
$output = shorturl($input); echo "Input : $input\n";
echo "Output : {$output[0]}\n";
echo " {$output[1]}\n";
echo " {$output[2]}\n";
echo " {$output[3]}\n";
echo "\n"; $input = 'http://www.snippetit.com/2';
$output = shorturl($input); echo "Input : $input\n";
echo "Output : {$output[0]}\n";
echo " {$output[1]}\n";
echo " {$output[2]}\n";
echo " {$output[3]}\n";
echo "\n";
Output: Input : http://www.snippetit.com/1
Output : h0xg4r
bdr3tw
osk2d3
4azfqa Input : http://www.snippetit.com/2
Output : tm5kxb
ceoj2s
yw3dvl
nrmrxl

  

The function return an array of 4 elements, you can use any one of them. The others can be used as alternative unique code for the input when you found a duplicated code in your database (same code but different input - although it is unlikely to happen but it will happen). Chances to get a duplicated code is about n/(32^6) or n/1,073,741,824 where n is the number of records in your database.

As you can see, the output results are quite random although you only have one character different in the input string. The output is always consistent, for the same input you will always get the same output.

To make the output more unpredictable by the others, you can scramble the values in the $base32 array or/and add in your own private key or/and XOR the value of $val with a value from range 0 to 31.

For example to scramble the values in the $base32 array, you can change the position of the values or/and replace the value with another (make sure the replaced value is URL safe character).

For example to add in private key, you can add in additional string when calling the md5() function, e.g.:

$hex = md5('my-secret-key'.$input.'my-another-secret-key');
For example to XOR the value of $val with value of 18:

$out .= $base32[$val ^ 18];

PHP: Short URL Algorithm Implementation的更多相关文章

  1. 短网址(short URL)系统的原理及其实现

    短网址(short URL)系统的原理及其实现 https://hufangyun.com/2017/short-url/?hmsr=toutiao.io&utm_medium=toutiao ...

  2. URL及short URL短网址

    URL,uniform resource locator,经常被称为网址,尤其是在使用HTTP的时候.通常是一个指向某个资源的字符串.   URLs经常被用于网页(http),但也可以用于文件传输(f ...

  3. short URL 短网址实现原理剖析

    short URL 短网址实现原理剖析 意义,简短便于分享,避免出现超长 URL 的字符长度限制问题 原理分析, 使用 HashMap 存储对应的映射关系 (长度不超过7的字符串,由大小写字母加数字共 ...

  4. short url短链接原理

    一.什么是短链接 含义:就是把普通网址,转换成比较短的网址.比如:http://t.cn/RlB2PdD 这种,比如:微博:这些限制字数的应用里都用到这种技术. 优点:短.字符少.美观.便于发布.传播 ...

  5. [Algorithm] Maximum Contiguous Subarray algorithm implementation using TypeScript / JavaScript

    Naive solution for this problem would be caluclate all the possible combinations: const numbers = [1 ...

  6. 百度 谷歌 Twitter,这么多短链接服务(Short Url)究竟哪家强?

    一.短链接是什么 url=HPqdQ5VR3vA39x7ZWoWyNzwWnsDhTbh66BTpdzsJLroBDzFRm4JV-G818Zc027uZrwe7zxtxnD4H2FUahftpUK& ...

  7. [Algorithm] Median Maintenance algorithm implementation using TypeScript / JavaScript

    The median maintenance problem is a common programming challenge presented in software engineering j ...

  8. Design Tiny URL

    Part 1: 前言: 最近看了一些关于短址(short URL)方面的一些博客,有些博客说到一些好的东西,但是,也不是很全,所以,这篇博客算是对其它博客的一个总结吧. 介绍: 短址,顾名思义,就是把 ...

  9. [LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

随机推荐

  1. MySQL DROP DB或TABLE场景下借助SQL Thread快速应用binlog恢复方案

    [问题] 假设有这种场景,误操作DROP DB或TABLE,常规的恢复操作是还原全备份,并用mysqlbinlog追加到drop操作前的位置. 如果需要恢复的binlog的日志量比较大而我们只希望恢复 ...

  2. 新手通过SVN向eclipse中导入项目注意事项

    该文章进行的前提是,jdk.eclipse.tomcat.maven已安装完成 要从svn上获取项目数据,首先要安装svn 1)通过help->installsoft->svn->a ...

  3. 开发自己的山寨Android注解框架

    目录 开发自己的山寨Android注解框架 开发自己的山寨Android注解框架 参考 Github黄油刀 Overview 在上一章我们学习了Java的注解(Annotation),但是我想大家可能 ...

  4. PHP菜刀工具WebHandler

    PHP菜刀工具WebHandler   在Web渗透测试中,后台代码如果包含系统命令执行功能,并以用户提交的数据作为参数,就带来潜在的安全隐患.Kali Linux提供一款PHP菜单工具WebHand ...

  5. 我的Python之旅第五天---迭代器和生成器

    h3,#nv_portal .vw .d .h3 {display: block; font-weight: 500; background-image: linear-gradient(to rig ...

  6. [CQOI2009]跳舞

    思路:二分答案+最大流.二分答案$m$,表示最多跳$m$轮.将每个人拆成两个点$a_i$$b_i$,$a_i$表示与任何人跳舞,$b_i$表示与不喜欢的人跳舞.对于第$i$个人,连一条从$a_i$到$ ...

  7. 【BZOJ-2142】礼物 拓展Lucas定理

    2142: 礼物 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1313  Solved: 541[Submit][Status][Discuss] ...

  8. 【GDKOI 2016】地图 map 类插头DP

    Description 对于一个n*m的地图,每个格子有五种可能:平地,障碍物,出口,入口和神器.一个有效的地图必须满足下列条件: 1.入口,出口和神器都有且仅出现一次,并且不在同一个格子内. 2.入 ...

  9. Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2)

    Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2) #include <bits/stdc++ ...

  10. Mac应用

    App Store 安装: AnappyApp:   截图软件 Snap:  Dock快捷键启动 izip Unarchiver: rar解压 Dr.Cleaner:内存清理.资源监控 下载安装: C ...