Generating short keys from large ID numbers

Discussion in 'PHP' started by gbh, Apr 9, 2009.

  1. #1
    gbh, Apr 9, 2009 IP
  2. googlehelper

    googlehelper Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    just use md5() with long url as the function input to generate short key
    store the short key with url in a table
    when someone accesses short url check the short code in the table and find out the long url
     
    googlehelper, Apr 9, 2009 IP
  3. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #3
    matthewrobertbell, Apr 9, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    They are not hashes of the URL.

    Those sites just use a sequential ID number for each URL they store, and encode that ID number to create the "shortened" URL. So instead of trying to hash a 50-character URL, they're just encode a 7-or-so-digit number.
     
    SmallPotatoes, Apr 9, 2009 IP
  5. amine

    amine Active Member

    Messages:
    47
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    93
    #5
    <?php
    $key_chars = 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", "6", "7", "8", "9" ) ;
    $length=6;
    $text="";
    for ($i = 0; $i < $length; $i++) {
        $text .= $key_chars[rand(0, count($key_chars)-1)];
    }
    echo $text;
    ?>
    PHP:
     
    amine, Apr 9, 2009 IP
  6. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #6
    How are you going to to decode that though? It's random, you're not actually encoding any useful information.
     
    matthewrobertbell, Apr 10, 2009 IP