character error when decoding a variable from js to php

Asked

Viewed 472 times

1

Good morning, I’m a problem that I do not know the reason, I have a code encoded in Base64 and I inserted this code into a js variable, from php to js, it worked more perfect impossible, ai I passed this js variable to php, also worked beauty, it displays exactly the code after encoding, the problem was when decoding

I encoded like this:

$encode = base64_encode(salt.hash));

I passed to the js so:

window.localStorage.setItem("campo", "<?php echo $encode; ?>");

I switched to PHP like this:

$decode_js = "<script>document.write(localStorage.getItem('campo'))</script>";

I decoded it and displayed it like this:

echo $decode = base64_decode($decode_js);

In this echo, what it brings is something very monstrous:

That:

MGszbGk5MGlvb2Y0ZWhwdXNkc2JhYWNmOTNlZTUyZTBlNjI0ZmQ0NzNkZjUyYjVlZDE5N2Y0ODM5NTQ0OWZmN2YxZjY0ZDg2ZDgzZWQ0YjBlNmRlNTFiYTk2ZmQ2YjA1ZjU1MjI5MTFmM2Q2OWI5N2RiMGUwMzhlYTg0OWZmYzBjZThiYWQxMDMxZjUzMmRkYWI0NzFiOTAzZA

Turned out to be this:

�����hr鞞�+�ץ�ƥJ�+j�z�-zh�>)�����

Can anyone help me with anything? Grateful from now on

  • Have you seen if the value of decode_js is exactly the field value? Because I think you are decoding the "<script>" too

  • because @khaosdoctor also found this, and for that not to occur? Is there another way to pass the js variable to a php variable? because if I decode it before, it works, of no

  • You cannot use javascript inside PHP this way. PHP is a server language, and its execution is performed before any data reaches the browser. To send a variable to PHP, you can send a form or, in the case of a page refresh not possible, use AJAX.

  • I think the means would be for you to create a cookie... It’s one of the simplest means.

  • @Khaosdoctor the problem is precisely, can not use cookie, IE hangs cookie by default, so this problem that made me think of this code up there, I caught on at this point

  • @brunoapimentel in this case, I would make an ajax to pass the variable to php as if it were a page refresh with method post in the case?

Show 1 more comment

1 answer

2


The problem is that you are decoding the javascript function and not the Document.write output, because php sends the information to the server first, then runs the javascript. I kept the output in javascript:


<script type="text/javascript">
<?php 
$salt_rash = 'seucodigo';
$encode = base64_encode($salt_rash);
?>

function setCodigo() {
   localStorage.getItem('campo', '<?php echo $encode?>');
}

function getCodigo() {
 var codigo = base64_decode_js(localStorage.getItem('campo'));
 document.write(codigo);
}

function base64_decode_js(data) {
 var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
    ac = 0,
    dec = '',
    tmp_arr = [];

  if (!data) {
    return data;
  }

  data += '';

  do { // unpack four hexets into three octets using index points in b64
    h1 = b64.indexOf(data.charAt(i++));
    h2 = b64.indexOf(data.charAt(i++));
    h3 = b64.indexOf(data.charAt(i++));
    h4 = b64.indexOf(data.charAt(i++));

    bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;

    o1 = bits >> 16 & 0xff;
    o2 = bits >> 8 & 0xff;
    o3 = bits & 0xff;

    if (h3 == 64) {
      tmp_arr[ac++] = String.fromCharCode(o1);
    } else if (h4 == 64) {
      tmp_arr[ac++] = String.fromCharCode(o1, o2);
    } else {
      tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
    }
  } while (i < data.length);

  dec = tmp_arr.join('');

  return dec.replace(/\0+$/, '');
}
</script>

<body onload="setCodigo();getCodigo();">
  • got it, web that use ajax to catch with the post right?

  • Not quite that, look what I put in the answer.

  • I understand, do all this and then load on the body, I will study this code a lot, it is great, grateful

Browser other questions tagged

You are not signed in. Login or sign up in order to post.