Creating your own TinyURL

前端 未结 13 2130

I have just found this great tutorial as it is something that I need.

However, after having a look, it seems that this might be inefficient. The way it works is, fir

相关标签:
13条回答
  • 2020-12-13 12:14

    I wouldn't bother doing ordered enumeration for two reasons:

    1) SQL servers are very effective at checking such hash collisions (given correct indexes)

    2) That might hurt privacy, as users would be able to easily figure out what other users are tinyurl-ing.

    0 讨论(0)
  • 2020-12-13 12:16

    The tiny url people like to use random tokens because then you can't just troll the tiny url links. "Where does #2 go?" "Oh, cool!" "Where does #3 go?" "Even cooler!" You can type in random characters but it's unlikely you'll hit a valid value.

    Since the key is rather sparse (4 values each having 36* possibilities gives you 1,679,616 unique values, 5 gives you 60,466,176) the chance of collisions is small (indeed, it's a desired part of the design) and a good SQL index will make the lookup be trivial (indeed, it's the primary lookup for the url so they optimize around it).

    If you really want to avoid the lookup and just unse auto-increment you can create a function that turns an integer into a string of seemingly-random characters with the ability to convert back. So "1" becomes "54jcdn" and "2" becomes "pqmw21". Similar to Base64-encoding, but not using consecutive characters.

    (*) I actually like using less than 36 characters -- single-cased, no vowels, and no similar characters (1, l, I). This prevents accidental swear words and also makes it easier for someone to speak the value to someone else. I even map similar charactes to each other, accepting "0" for "O". If you're entirely machine-based you could use upper and lower case and all digits for even greater possibilities.

    0 讨论(0)
  • 2020-12-13 12:18

    Couldn't you encode the URL as Base36 when it's generated, and then decode it when visited - that would allow you to remove the database completely?

    A snippet from Channel9:

    The formula is simple, just turn the Entry ID of our post, which is a long into a short string by Base-36 encoding it and then stick 'http://ch9.ms/' onto the front of it. This produces reasonably short URLs, and can be computed at either end without any need for a database look up. The result, a URL like http://ch9.ms/A49H is then used in creating the twitter link.

    0 讨论(0)
  • 2020-12-13 12:19

    You could of course add ordering by simply numbering the urls:

    http://mytinyfier.com/1
    http://mytinyfier.com/2
    

    and so on. But if the hash key is indexed in the database (which it obviously should be), the performance boost would be minimal at best.

    0 讨论(0)
  • 2020-12-13 12:24

    Check out this guys functions - http://www.pgregg.com/projects/php/base_conversion/base_conversion.php source - http://www.pgregg.com/projects/php/base_conversion/base_conversion.inc.phps

    You can use any base you like, for example to convert 554512 to base 62, call

    $tiny = base_base2base(554512, 10, 62); and that evaluates to $tiny = '2KFk'.

    So, just pass in the unique id of the database record.

    In a project I used this in a removed a few characters from the $sChars string, and am using base 58. You can also rearrange the characters in the string if you want the values to be less easy to guess.

    0 讨论(0)
  • 2020-12-13 12:25

    Use autoincrement on the database, and get the latest id as described by http://www.acuras.co.uk/articles/24-php-use-mysqlinsertid-to-get-the-last-entered-auto-increment-value

    0 讨论(0)
提交回复
热议问题