views:

35

answers:

1

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.

+2  A: 

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.

Chacha102
You could always do a one-time pass and store everything in the database the first time you decide to change encodings methods. There's something to be said for not incurring a database query for every 404 in the meantime.
Frank Farmer