Home > Net >  PHP base64_encode giving different result to Javascript btoa
PHP base64_encode giving different result to Javascript btoa

Time:05-25

I have the dummy account:
Username: [email protected]
Password: Av3$truz

When I try to use PHP base64_encode using the code base64_encode($userName . ":" . $password);

I get "Z2lhbmNhZ2FsbGFyZG9AZ21haWwuY29tOkF2Mw"

When I use Javascript BTOA using the code btoa(userName ":" password) I get "Z2lhbmNhZ2FsbGFyZG9AZ21haWwuY29tOkF2MyR0cnV6".

I am supposed to be getting the second one in PHP, what am I doing wrong?

CodePudding user response:

Since this is your code:

$userName = "[email protected]";
$password = "Av3$truz";

echo base64_encode($userName . ":" . $password);

PHP is trying to get the value of $truz, change it to this:

$userName = "[email protected]";
$password = 'Av3$tru'; //now it does not try to evaluate it

echo base64_encode($userName . ":" . $password);

CodePudding user response:

I get the same results.

echo base64_encode('hello' . ':' . 'how are you?');

PHP eval

alert(btoa('hello'   ':'   'how are you?'));

JS Fiddle

The result is in both cases:

aGVsbG86aG93IGFyZSB5b3U/

In case you don't get the same, can you give an example, just like I did?

Note that character encoding might be a factor here, but it is probably more likely that your variables don't have the same content.

  • Related