Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
54
rated 0 times [  60] [ 6]  / answers: 1 / hits: 21943  / 7 Years ago, fri, february 10, 2017, 12:00:00

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



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?



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



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


More From » php

 Answers
53

So in javascript (updated with encoding URI):



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


btoa example



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



Then in PHP, after you retrieve the request:



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


base64_decode docs


[#58983] Wednesday, February 8, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
breap

Total Points: 606
Total Questions: 96
Total Answers: 108

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
;