When using a unique alphanumeric string for a short url, is it better to store the created string in the database or encode/decode on the fly?

丶灬走出姿态 提交于 2019-12-11 06:04:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!