1

I'm trying to encode my script using base64 in php, but why my code automatic adds backslashes \ at single quotes ' or double quotes "
this is the code I am using to encode

$encode = $_POST ['encode'];
$encod = base64_encode ($encode);
echo "<form action='?' method='post'><textarea name='encode'></textarea><input type='submit' value='Encode'></form><textarea> ".$encod." </textarea>";

I use code at above, then I try to encode this script:

echo "just test";
echo 'or just test'; 

and result

PD9waHAgZWNobyBcImp1c3QgdGVzdFwiOw0KZWNobyBcJ29yIGp1c3QgdGVzdFwnOyA/Pg==

when I decode, result

echo \"just test\";
echo \'or just test\';

how to delete backslashes??

I've tried using str_replace on $encod, and stripslashes($encod) but it does not work.

aksu
  • 5,221
  • 5
  • 24
  • 39
user3367454
  • 11
  • 1
  • 2
  • How did you used the `str_replace` to replace ? Please include the codes you have tried to accomplish the task – Subin Mar 01 '14 at 06:59
  • Replace \' $encod = str_replace("\'","",$encod); replace \" $encode = str_replace('\"','',$encod); – user3367454 Mar 01 '14 at 07:03
  • Is this really caused by `base64_encode`, or are the backslashes in your original string (`$encode`)? – pat Mar 01 '14 at 07:09
  • What's your encode var value – guy_fawkes Mar 01 '14 at 07:09
  • 1
    @user3367454 I tried decoding `PD9waHAgZWNobyBcImp1c3QgdGVzdFwiOw0KZWNobyBcJ29yIGp1c3QgdGVzdFwnOyA/Pg==`, but it's not valid. – Subin Mar 01 '14 at 07:10
  • @Subin, I decoded it and got `` – pat Mar 01 '14 at 07:11
  • I have tried to decode "PD9waHAgZWNobyBcImp1c3QgdGVzdFwiOw0KZWNobyBcJ29yIGp1c3QgdGVzdFwnOyA/Pg==" but its doesnt give result. You can get rid of the backslashes by doing html_entity_decode on the output – guy_fawkes Mar 01 '14 at 07:31

2 Answers2

1

I have tried decoding your base64 encoded string and it yielded the same result. I tried encoding it again, slashes and all, and it yielded the same encoded string.

The problem is not that you are somehow getting unknown slashes out of the decoding process, but that the slashes exist there before you ever encoded the string.

The same string without slashes is this: PD9waHAgZWNobyAianVzdCB0ZXN0IjsNCmVjaG8gJ29yIGp1c3QgdGVzdCc7ID8+

Try decoding it.

I believe that the real solution to your problem can be found here:

Why are $_POST variables getting escaped in PHP?

Community
  • 1
  • 1
flagoworld
  • 3,196
  • 2
  • 20
  • 16
  • So how the complete code, I tried to use it to encode, but it generates ¦^r#ºËmzË^r(®;¬¶×¬ Sorry for bad english – user3367454 Mar 01 '14 at 07:54
0

I believe the string you are encoding in base64 was already encoded in json (because the POST has to be done with a json string, I assume).

Json's way to escape the quotes is what you describe to be your problem:

See: json_encode PHP

So the solution here would be to first decode from base64 (base64_decode [PHP]) then decode from json (jsoN_decode() [PHP]) or (JSON.parse() [JS])

DereckM
  • 274
  • 2
  • 11