PHP中URL的编码和压缩
如何在 PHP 中轻松编码和“压缩”URL/电子邮件地址为字符串?
字符串应该是:
- 难以被用户解码
- 尽可能短(压缩)
- 相似的 URL 应该不同 编码后
- 不在数据库中,
- 易于通过 PHP 脚本进行解码/解压缩
。 输入-> 输出, stackoverflow.com/1/ -> “n3uu399”, stackoverflow.com/2/-> “ojfiejfe8”
How to easy encode and "compress" URL/e-mail adress to string in PHP?
String should be:
- difficult to decode by user
- as short as possible (compressed)
- similar URLs should be different
after encoding - not in database
- easy to decode/uncompress by PHP script
ex. input -> output,
stackoverflow.com/1/ -> "n3uu399",
stackoverflow.com/2/ -> "ojfiejfe8"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不是很短,但您可以使用密码 zip 它并使用 < a href="http://php.net/manual/en/function.base64-encode.php" rel="nofollow noreferrer">base64。 请注意,zip 对于密码来说不太安全,但应该如果您的加密值的生命周期较短,那也没关系。
请注意,无论您做什么,除非您同意在本地存储一些无法访问的信息,否则您将无法生成某种程度上安全的编码。 这意味着,无论您做什么,都认为任何人都可以在足够的时间内访问伪加密数据,无论是通过逆向工程您的算法、暴力破解您的密码还是其他必要的方式。
Not very short but you could zip it with a password and encode it using base64. Note that zip is not too safe when it comes to passwords, but should be ok if your encrypted value is intended to have a short lifetime.
Note that whatever you do, you won't be able to generate a somewhat safe encoding unless you agree to store some unaccessible information locally. This means, whatever you do, take it as given that anyone can access the pseudo-encrypted data with enough time, be it by reverse engineering your algorithm, brute-forcing your passwords or whatever else is necessary.
您可以基于常见字符串创建自己的文本压缩系统:如果 URL 开头为“http://www.”,则缩短的 URL 的第一个字符是'a',如果它以 'https://www.' 开头,则第一个字符是 'b'...(流行变体重复),如果不是,则第一个字母是“z”,并且 url 遵循编码模式。
如果接下来的三个字母是“abc”,则第二个字母是“a”等。您需要列出 URL 中最常见的字母对/三元组,并计算出最流行的 26/50 等(取决于哪个)你想要使用的字符),并且你应该能够完全在 PHP 中对 URL 进行一些压缩(不使用数据库)。 人们只能通过了解您的字母对/三元组列表(您的映射列表)或手动对其进行逆向工程来逆向它。
You could make your own text compression system based on common strings: if URL starts 'http://www.', then the first character of the shortened URL is 'a', if it starts 'https://www.', then the first character is 'b'...(repeat for popular variants), if not then first letter is 'z' and the url follows in a coded pattern.
The if next three letters are 'abc', the second letter is 'a' etc. You'll need a list of which letter pairs/triplets are most common in URLs and work out the most popular 26/50 etc (depending on which characters you want to use) and you should be able to conduct some compression on the URL entirely in PHP (without using a database). People will only be able to reverse it by either knowing your letter pair/triplet list (your mapping list) or by manually reverse-engineering it.
这是一个简单的实现,可能会也可能不会满足您的需求:
输入/输出:
代码:
耸耸肩
您需要更具体地了解您的输入和输出以及您对每个输出的期望。
您还可以使用 gzcompress/gzuncompress 而不是序列化/反序列化等。
Here is a simple implementation that may or may not fulfil your needs:
Input/Output:
Code:
shrug
You need to be more specific about your inputs and outputs and what you expect from each.
You could also use gzcompress/gzuncompress instead of serialize/unserialize, etc.
如果您有权访问数据库,那么您可以进行关系查找,即有 2 个字段,第一个字段保存原始 URL,第二个字段保存压缩 URL。
要创建第二个 URL,您可以执行类似以下操作
这只是我想到的一个想法,代码未经测试
if you have access to a database then you could do a relational lookup i.e. there will be 2 fields, field one holding the original URL and the second holding the compressed URL.
To make the second URL you could do something like the following
This is just an idea that i have thought up, code isn't tested