如何在 base64 中对字符串进行编码以在 URL 中使用?

How to encode a string in base64 to be used in a URL?

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

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

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>";