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

问题描述:

我认为这将是一个简单的Google搜索,但看来我的技术并不像我想的那么普遍.

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字符串包含很多/+=decode在PHP中完全相同的字符串,以便将其存储在数据库中?

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.

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

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编码的字符串添加到您的get请求中.

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文档