How do the following sites make such short keys? http://tinyurl.com/djf7u5 http://bit.ly/dcj7i http://digg.com/d1oHPZ I'll give anyone who can help a digital beer
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
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.
<?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:
How are you going to to decode that though? It's random, you're not actually encoding any useful information.