In a .php
file I have some PHP code which is encoded with Base64, and it decodes fine with some base64_decode
code that is in a .js
file.
However in the same .php
file using JavaScript, I have been calling the same PHP code. Even though this is still being encoded, it does not appear to be decoded from the same .js
file.
<?php
$items_folder="http://domain/items/";
$items_folder_enc=base64_encode($items_folder);
?>
<script type='text/javascript'>
var itemsnew=<?php echo json_encode($items_folder_enc); ?>;
What can I change so that the <?php echo json_encode($items_folder_enc); ?>;
is decoded like its PHP counterpart?
If you want to decode server-side and send $items_folder
to client as plain text then:
var itemsnew = "<?php echo base64_decode($items_folder_enc); ?>";
If you want to decode client-side and send $items_folder
to client in an "encoded" manner then:
var itemsnew = atob("<?php echo $items_folder_enc; ?>");
In the later case, you might want to check for atob support and create a base64 decoding user function if browser doesn't support it.