用 JavaScript 编码 Base64,用 GET 发送,用 PHP 解码

2021-12-27 00:00:00 encoding base64 get php javascript

我认为这将是一个简单的谷歌搜索,但我的技术似乎并不像我想象的那么普遍.

I thought this will be an easy Google-search, but it seems that my technique is not as common as I thought.

我如何在 JavaScript 中 encode 一个 Base64 字符串,以便将它安全地放在带有 GET 参数的行上(我的 Base64 字符串包含很多/+=decodePHP中的相同字符串,以便将其存储在数据库中?

How can I encode a Base64 string in JavaScript, in order to put it safely on the line with a GET parameter (my Base64 string contains a lot of /, + and =, and decode the exact same string in PHP, in order to store it in a database?

当我发布没有编码的 Base64 字符串时,我的 PHP 脚本不接受它.

When I post the Base64 string without encoding, my PHP script does not accept it.

我找到了一个解决方案来编码和解码Base64字符串在 JavaScript 中,但对我来说没有意义.

I found a solution for encoding and decoding the Base64 string in JavaScript, but not in the way it makes sense for me.

推荐答案

所以在 javascript 中(更新为编码 URI):

So in javascript (updated with encoding URI):

var myStr = "I am the string to encode";
var token = encodeURIComponent(window.btoa(myStr));

btoa 示例

然后您应该能够将该 base64 编码的字符串添加到您的获取请求中.

You should be then able to add that base64 encoded string to your get request.

然后在 PHP 中,在您检索请求之后:

Then in PHP, after you retrieve the request:

<?php
   $str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
   echo base64_decode(urldecode($str));
?>

base64_decode 文档

相关文章