使用JavaScript编码Base64,使用GET发送,使用PHP解码 [英] Encode Base64 in JavaScript, send with GET, decode in PHP

查看:176
本文介绍了使用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文档

这篇关于使用JavaScript编码Base64,使用GET发送,使用PHP解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆