• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Software and Hardware
  • New and Happening
You are here: Home / Programming and Scripting / PHP: Short URL Algorithm Implementation

PHP: Short URL Algorithm Implementation

April 10, 2009 by Sze Hau 9 Comments

Few months ago, I introduced a simple algorithm that allow users to implement their own short URL into their system. Today, I have some spare time so I decided to write the short URL algorithm’s implementation in PHP.

At first, we define a function called shorturl() that receives a URL as the input and returns an array that contains 4 hashed values (each 6 characters).

function shorturl($input) {
   ...
  // return array of results
}

 

Below is the original pseudocode:

...
  loop2: from 1st 4 bytes to 4th 4 bytes of md5 result
    cast the 4 bytes to an integer
    loop3: for shortCodeChar[0] to shortCodeChar[5]
      use 1st 5 bits of the integer to find the value in codeMap
      remove 5 bits from the integer
    end loop3
    save shortCodeChar as shortCode
    ...
    // Database checking for duplication
  end loop2
...

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];

More from my site

  • Short URL: Top 5 Websites That Provide Free Short URL ServiceShort URL: Top 5 Websites That Provide Free Short URL Service
  • Implement your own short URLImplement your own short URL
  • BigDump: Alternative to phpMyAdmin to Import/Restore Large DatabaseBigDump: Alternative to phpMyAdmin to Import/Restore Large Database
  • PHP: How to Protect Password in Database – Password HashingPHP: How to Protect Password in Database – Password Hashing
  • PHP: Format integer into number of decimal placesPHP: Format integer into number of decimal places
  • PHP: session_start() – No such file or directory errorPHP: session_start() – No such file or directory error

Filed Under: Programming and Scripting Tagged With: PHP, short URL, tiny URL

About Sze Hau

Geek. Love programming. Coffee addicted. Married with two children. Working towards financial freedom.

Trackbacks

  1. Short Url(短网址)实现方式 | [email protected] says:
    April 27, 2009 at 2:18 pm

    […] 另一个算法来自http://www.snippetit.com/2009/04/php-short-url-algorithm-implementation/ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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; } […]

  2. Short URL (短网址) 算法C#实现 - 闲云博客 | 关注互联网科技,记录编程点滴 says:
    September 4, 2010 at 9:42 am

    […] 下面先来看看短网址映射算法的理论(网上找到的资料http://www.snippetit.com/2009/04/php-short-url-algorithm-implementation/) […]

  3. 一个php的短网址实现算法 | Akii Snow says:
    October 21, 2010 at 5:34 pm

    […] 原文在这里:http://www.snippetit.com/2009/04/php-short-url-algorithm-implementation/ This entry was posted in PHP & MYSQL. Bookmark the permalink. ← 第一次听见我的mbp风扇响,居然是因为网游广告 […]

  4. 网上各种短网址(ShortUrl)算法应用的初步详细汇总 | 空的博客 says:
    March 16, 2012 at 6:58 pm

    […] 一个来自http://www.snippetit.com/2009/04/php-short-url-algorithm-implementation/ , 代码如下: <?php 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' ); […]

  5. 网上各种短网址算法应用的初步详细汇总 | PHP10086博客网 says:
    March 31, 2012 at 9:00 am

    […] 一个来自http://www.snippetit.com/2009/04/php-short-url-algorithm-implementation/ , 代码如下: <?php  function shorturl($input) {  $base32 = array (  'a', 'b', […]

  6. 网上各种短网址算法应用的初步详细汇总[转] | 钟代麒 says:
    October 19, 2012 at 8:00 pm

    […] 一个来自http://www.snippetit.com/2009/04/php-short-url-algorithm-implementation/ , 代码如下: <?php  function shorturl($input) {  $base32 = array (  'a', 'b', […]

  7. cnblogs: PHP: Short URL Algorithm Implementation – Athrun | 易站|工作室 says:
    August 21, 2013 at 11:30 pm

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

  8. 如何构建短网址服务 | 名哲博客 says:
    December 30, 2014 at 1:48 pm

    […] http://www.snippetit.com/2009/04/php-short-url-algorithm-implementation/ […]

  9. php 短链接算法收集与分析-天长人才网 says:
    July 9, 2016 at 11:16 am

    […] 5、PHP Short Url Algorithm Implementation […]

Leave a Reply Cancel reply

Advertisement

  • Facebook
  • Google+
  • Instagram
  • Twitter

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

Software and Hardware

MD5 and SHA1 Checksum Using Windows

July 5, 2017 By Sze Hau Leave a Comment

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • szehau's weblog Life, Internet, Software, Gadgets, Programming and Investments

Snippet IT

This is the place where I want to share anything about information technology.

Search

Recent

  • MD5 and SHA1 Checksum Using Windows
  • MD5 and SHA1 Checksum Using Linux
  • Java: Unlimited Strength Jurisdiction Policy
  • WordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And Files

Tags

Adsense advertisement advertising apache blog blogging tips C# EGPC error estimation format format Integer Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP internet marketing Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL SQL static constructor String tiny URL Tips and Tricks twitter video Windows Vista Wordpress wordpress plugin wordpress theme Youtube

Copyright © 2023 · Magazine Pro Theme on Genesis Framework · WordPress · Log in