I have a list of image paths in my PHP script that I would like to pass to javascript whitout rendering them in the HTML page. I mean, I don’t want people go fishing for the path’s when do do a > view HTML source.
<?php
$images_str = "some/dir/001.jpg|*|some/dir/002.jpg|*|some/dir/003.jpg";
$images_arr = array('some/dir/001.jpg', 'some/dir/002.jpg', 'some/dir/003.jpg');
?>
<html>
<body>
<script type="text/javascript">
var dynamicID = 1;
/* String */
_images_str = "<?= $images_str ?>";
_images_str_arr = _images_str.split("|*|");
// alert(_images_str_arr[dynamicID]); // OK but renders the image paths in javascript
/* Array */
var _images_arr = new Array();
_images_arr = "<?= $images_arr ?>";
// alert("<?= $images_arr ?>"); // "Array"
// alert(_images_arr); // "Array"
// alert(_images_arr[1]); // "r" from "Array"
// alert("<?= $images_arr[1] ?>"); // "some/dir/002.jpg" works! but how to use dynamicID??
// alert("<?= count($images_arr) ?>"); // works as well
</script>
</body>
</html>
What are you going to do with those image paths in your javascript? If the final goal is to use them as the source of an
imgtag then you could do absolutely nothing to hide them as tools such as Firebug will show directly all the HTTP requests that the browser performs, so it’s not even necessary to look at the source of the HTML page to obtain the image paths.If you intend to do something else with these paths (??) you could use a public/private key encryption algorithm. For example you generate a pair of private/public keys in javascript and you use ajax to send the public key to your server script. The script uses this public key to encrypt the image paths and returns them as JSON array to the client script which uses it’s private key to decrypt them.
UPDATE:
Here’s one example of sending the list of paths through AJAX:
and you obtain them in javascript: