PHP中URL的编码和压缩

发布于 2024-07-27 18:01:39 字数 298 浏览 3 评论 0原文

如何在 PHP 中轻松编码和“压缩”URL/电子邮件地址为字符串?

字符串应该是:

  1. 难以被用户解码
  2. 尽可能短(压缩)
  3. 相似的 URL 应该不同 编码后
  4. 不在数据库中,
  5. 易于通过 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:

  1. difficult to decode by user
  2. as short as possible (compressed)
  3. similar URLs should be different
    after encoding
  4. not in database
  5. easy to decode/uncompress by PHP script

ex. input -> output,
stackoverflow.com/1/ -> "n3uu399",
stackoverflow.com/2/ -> "ojfiejfe8"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青瓷清茶倾城歌 2024-08-03 18:01:39

不是很短,但您可以使用密码 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.

初心 2024-08-03 18:01:39

您可以基于常见字符串创建自己的文本压缩系统:如果 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.

慵挽 2024-08-03 18:01:39

这是一个简单的实现,可能会也可能不会满足您的需求:

输入/输出:

[email protected]    cyUzQTEzJTNBJTIydGVzdCU0MHRlc3QuY29tJTIyJTNC             [email protected]  
http://test.com/ cyUzQTE2JTNBJTIyaHR0cCUzQSUyRiUyRnRlc3QuY29tJTJGJTIyJTNC http://test.com/

代码:

function encode ($in) {
    return base64_encode(rawurlencode(serialize($in)));
}

function decode ($in) {
    return unserialize(rawurldecode(base64_decode($in)));
}

耸耸肩

您需要更具体地了解您的输入和输出以及您对每个输出的期望。

您还可以使用 gzcompress/gzuncompress 而不是序列化/反序列化等。

Here is a simple implementation that may or may not fulfil your needs:

Input/Output:

[email protected]    cyUzQTEzJTNBJTIydGVzdCU0MHRlc3QuY29tJTIyJTNC             [email protected]  
http://test.com/ cyUzQTE2JTNBJTIyaHR0cCUzQSUyRiUyRnRlc3QuY29tJTJGJTIyJTNC http://test.com/

Code:

function encode ($in) {
    return base64_encode(rawurlencode(serialize($in)));
}

function decode ($in) {
    return unserialize(rawurldecode(base64_decode($in)));
}

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.

枫以 2024-08-03 18:01:39

如果您有权访问数据库,那么您可以进行关系查找,即有 2 个字段,第一个字段保存原始 URL,第二个字段保存压缩 URL。
要创建第二个 URL,您可以执行类似以下操作

$str = "a b c d e f g h i j k l m n o p q r s t u v w x y z";

$str = explode(" ", $str);
$len = 5;

for($i = 0; $i < $len; $i++)
{
    $pos = rand(0, (count($str) - 1));
    $url .= $str[$pos];
}

这只是我想到的一个想法,代码未经测试

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

$str = "a b c d e f g h i j k l m n o p q r s t u v w x y z";

$str = explode(" ", $str);
$len = 5;

for($i = 0; $i < $len; $i++)
{
    $pos = rand(0, (count($str) - 1));
    $url .= $str[$pos];
}

This is just an idea that i have thought up, code isn't tested

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文