【问题标题】:How to encode a string in base64 to be used in a URL?如何编码 base64 中的字符串以在 URL 中使用?
【发布时间】:2017-09-20 19:07:15
【问题描述】:

我需要在电子邮件中附加一个可以包含多个部分和几乎random 字符的确认码。 这个想法是在消息正文中打印带有该代码的 URL(在 HTML 中)。

base64_encode() 函数是否足以使其安全地被浏览器解析?

【问题讨论】:

  • 您真的将二进制字符放入确认码中吗?如果只是字母和数字,则无需编码。
  • 是的。不幸的是,我没有选择该代码的格式...

标签: php base64 encode


【解决方案1】:

Base64 编码是取二进制数据,使其适合作为文本处理。

这不是你的问题(你说你有随机的字符)所以你不应该使用 Base64。

您有文本并希望将其插入 URL。您需要对其进行 URL 编码。这就是urlencode() function 的用途。

然后您想将该 URL 插入 HTML 文档中。这就是htmlspecialchars() function 的用途。

$data = function_to_get_random_data();
$url_safe_data = urlencode($data);
$url = "http://example.com/$url_safe_data";
$html_safe_url = htmlspecialchars($url);
$html = "<a href=\"$html_safe_url\">$html_safe_url</a>";

【讨论】:

    猜你喜欢
    • 2010-11-25
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 2011-10-31
    • 2010-09-19
    • 2015-06-04
    相关资源
    最近更新 更多