在这篇快速文章中,我们将讨论 php 上下文中 base64 编码的基础知识。基本上,我们将了解如何使用 PHP 函数将数据转换为 base64 编码格式。
什么是 Base64 编码?
Base64 是 ASCII 中二进制数据的编码。字符串中的每个字符代表六位信息。实际上,它以 base 64 表示形式表示二进制数据。如果您需要复习,请查看这本关于数字系统和基础的入门书。
Base64 编码的主要用途是将二进制数据编码为 ASCII 文本,当您希望通过专门设计用于处理文本数据的协议传输此类数据时。这可确保数据在传输时保持完整。具体来说,Base64 用于发送电子邮件和上传 html 表单中的二进制数据。
Base64 编码的另一个常见用途是散列。当然,您不应该使用 Base64 编码本身来生成哈希,因为它很容易被解码。首先,您借助 SHA 等散列算法生成散列,然后将生成的散列转换为 Base64 编码格式以显示它。比较两个 Base64 编码的校验和的完整性非常容易。
在下一节中,我们将讨论 PHP 中的内置 Base64 函数。
PHP 中的 Base64 函数
PHP 中有两个主要的函数处理 Base64 编码。该base64_encode
函数允许您以 MIME Base64 格式对数据进行编码。另一方面,该base64_decode
函数用于解码 MIME Base64 编码的数据。
让我们详细介绍这些功能。
base64_encode
让我们看一下base64_encode
函数的语法。
base64_encode ( string $string ) : string
您需要传递给base64_encode
函数的唯一参数是要编码为 MIME Base64 的源字符串。
这将返回一个 Base64 编码的字符串。需要注意的是,Base64 编码的数据比原始数据多占用大约 33% 的内存空间。
base64_decode
让我们看一下base64_decode
函数的语法。
base64_decode ( string $string , bool $strict = false ) : string|false
第一个参数是要解码的 Base64 编码数据。
第二个参数是可选的,但如果你通过TRUE
,它将执行严格检查。这意味着如果 Base64 编码的数据包含来自 Base64 字母表之外的字符,则 decode 函数将返回FALSE
. 这对于检查 Base64 编码字符串的完整性很有用。另一方面,如果您想静默丢弃无效字符,只需传递FALSE
默认值即可。
解码成功后,函数返回解码后的字符串,否则返回FALSE
. 需要注意的是,base64_decode
如果所讨论的字符串在编码之前是二进制格式,则该函数可能会返回二进制数据。
在下一节中,我们将讨论如何在 PHP 中使用内置的 Base64 函数。
如何使用 MIME Base64 编码和解码数据
在本节中,我们将通过几个示例来了解 Base64 函数的实际应用。
编码 URL 参数
通常情况下,您最终会使用该base64_encode
函数对包含 URL 的参数进行编码。
让我们通过以下示例快速了解它是如何工作的。
<?php $redirectUrl = 'https://www.example.com/some/other/url'; header('Location: https://www.example.com/redirect/' . base64_encode($redirectUrl)); //https://www.example.com/redirect/aHR0cDovL3d3dy5leGFtcGxlLmNvbS9zb21lL290aGVyL3VybA== ?>
如您所见,如果我们不使用该base64_encode
函数,则生成的 URL 看起来像https://www.example.com/redirect/http://www.example.com/some/other/url
,这是无效的。
Base64 编码校验和
正如我们前面所讨论的,该base64_encode
函数在将二进制数据转换为 ASCII 字符串时会派上用场。
让我们看看下面的例子。
<?php $base64_encoded_hash = base64_encode(hash_hmac('sha1', 'Source string to be hashed.', 'YOUR_TOP_SECRET_KEY', true)); ?>
在上面的示例中,该hash_hMac
函数将返回散列的二进制数据。因此,如果不使用该base64_encode
函数,将很难显示生成的二进制数据。
电子邮件附件
在 PHP 中,当您发送带有文件附件的电子邮件时,您可以将文件内容编码为 Base64 编码格式。事实上,如果您要发送二进制附件,那么您必须对附件数据进行编码,而不是以原始格式发送。
让我们看看下面的例子。
<?php $to = 'receiver@example.com'; $subject = 'Example of base64 encoded attachments'; $message = 'This email contains an image attachment.'; $boundary = md5(microtime()); $headers = "From: name <from@example.com>" . "\r\n"; $headers .= "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-Type: multipart/mixed; boundary=" . $boundary . "\r\n"; $headers .= "Multipart MIME example." . "\r\n"; $message.= "--" . $boundary. "\r\n"; $message.= "Content-Type: text/plain; charset=\"iso-8859-1\"" . "\r\n"; $message.= "Content-Transfer-Encoding: 8bit" . "\r\n"; $message.= $message . "\r\n"; // attach file $filename = 'nature.jpg'; $filecontents = file_get_contents($filename); $filecontents = chunk_split(base64_encode($filecontents)); $message.= "--" . $boundary. "\r\n"; $message.= "Content-Type: image/jpg; name=\"" . $filename . "\"" . "\r\n"; $message.= "Content-Transfer-Encoding: base64" . "\r\n"; $message.= "Content-Disposition: attachment" . "\r\n"; $message.= $filecontents . "\r\n"; $message.= "--" . $boundary. "--"; mail($mailto, $subject, $body, $headers); ?>
如您所见,我们使用该base64_encode
函数在二进制图像数据作为附件发送之前对其进行编码。
通过这种方式,您可以将 PHP 中的 Base64 函数用于各种目的。
结论
在这篇快速文章中,我们讨论了如何在日常 PHP 开发中使用 Base64 函数。
- base64_encode
- base64_decode
- 编码 URL 参数
- Base64 编码校验和
- 电子邮件附件