Encode Base64 in JavaScript, send with GET, decode in PHP(用 JavaScript 编码 Base64,用 GET 发送,用 PHP 解码)
问题描述
我认为这将是一个简单的谷歌搜索,但我的技术似乎并不像我想象的那么普遍.
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.
我找到了一个解决方案来编码和解码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 文档
这篇关于用 JavaScript 编码 Base64,用 GET 发送,用 PHP 解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用 JavaScript 编码 Base64,用 GET 发送,用 PHP 解码


- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01