問題描述
我認為這將是一個簡單的谷歌搜索,但我的技術似乎并不像我想象的那么普遍.
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 解碼的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!