问题
I want to create shortened links for specific pieces of content on my site. To view these pages now, I pull the relevant content via the content ID passed via GET (ie, mysite.com/content/?id=332). To obfuscate the ID, I want to use base64 to encode and decode it into a short alphanumeric string (like 34sa6), which I already know how to do.
My question is this: does it make more sense to store this string as a database field on creation of each piece of content, or simply decode the string on the fly when a user visits mysite.com/content/34sa6 (which means visiting mysite.com/content/?id=332 will also load the correct page). If I store this instead, it will become the defacto primary key for my purposes, as all related content will be queried based on it, so just trying to figure out the wisest way to do it.
回答1:
If you decode and encode it during the request, you can't switch to a different method of encoding in the future.
Storing it in the database allows you to change the encoding whenever you want, because when the full URL needs to be retrieved, the script only looks for a matching database entry.
来源:https://stackoverflow.com/questions/2308579/when-using-a-unique-alphanumeric-string-for-a-short-url-is-it-better-to-store-t