Skip to content Skip to sidebar Skip to footer

Encode Base64 In Javascript, Send With Get, Decode In Php

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 safel

Solution 1:

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

Solution 2:

In Javascript

var password ="xyz"window.btoa(password)

In php

$password = urldecode(base64_decode($_POST['password']));

Post a Comment for "Encode Base64 In Javascript, Send With Get, Decode In Php"