在短网址中使用唯一的字母数字字符串时,将创建的字符串存储在数据库中还是即时进行编码/解码会更好吗? [英] 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?

查看:105
本文介绍了在短网址中使用唯一的字母数字字符串时,将创建的字符串存储在数据库中还是即时进行编码/解码会更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我网站上的特定内容创建缩短的链接.现在要查看这些页面,我通过GET传递的内容ID(即mysite.com/content/?id=332)提取相关内容.为了混淆ID,我想使用base64对其进行编码和解码,使其成为一个短的字母数字字符串(例如34sa6),我已经知道该怎么做.

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.

我的问题是:在创建每条内容时将此字符串存储为数据库字段是否更有意义,还是在用户访问mysite.com/content/34sa6时即刻对字符串进行即时解码(表示访问mysite.com/content/?id=332也将加载正确的页面).如果我存储它,它将成为事实上的事实上的主键,因为所有相关内容都将基于该主键进行查询,因此请尝试找出最明智的方法.

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.

推荐答案

如果在请求期间对其进行解码和编码,则以后将无法切换到其他编码方法.

If you decode and encode it during the request, you can't switch to a different method of encoding in the future.

将其存储在数据库中使您可以随时更改编码,因为当需要检索完整的URL时,脚本仅查找匹配的数据库条目.

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.

这篇关于在短网址中使用唯一的字母数字字符串时,将创建的字符串存储在数据库中还是即时进行编码/解码会更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆