I´m using an array, which I encode into json, than into BASE64, save it on a Cookie, to be later on retrieved. The actual string is:
{"insert":0,"delete":0}
In PHP I have:
$Sync = json_encode($update);
setcookie('Sync',strtr(base64_encode($Sync), '+/', '-_'), 0, "/");
The Cookie is being stored as:
eyJpbnNlcnQiOjEsImRlbGV0ZSI6MH0%3D
That final "%3D" should be "=" so to fill the rest of the base64, nad therefore return strange characters, but I can´t get it right. Any ideas? Thanks in advance
Within the docs on php.net (http://php.net/manual/en/function.base64-encode.php#103849) the top comment provides two functions that should help you (similar to your implementation as well :))
For anyone interested in the 'base64url' variant encoding, you can use this pair of functions:
<?php function base64url_encode($data) { return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); } function base64url_decode($data) { return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT)); } ?>
Within your implementation, it looks like you need to wrap strtr(base64_encode($Sync), '+/', '-_'), 0, "/")
with rtrim
, like so:
rtrim(strtr(base64_encode($Sync), '+/', '-_'), 0, "/"), '=')
Results: https://3v4l.org/5D1Mk