简体   繁体   English

PHP到Javascript,数组到数组,对象到对象

[英]PHP to Javascript, array to array, object to object

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. 我在PHP脚本中有一个图像路径列表,希望将这些图像路径传递给javascript,以便在HTML页面中呈现它们。 I mean, I don't want people go fishing for the path's when do do a > view HTML source. 我的意思是,我不希望人们在做>查看HTML源代码时去钓鱼。

<?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>

I don't want people go fishing for the path's when do do a > view HTML source 我不希望人们在做>查看HTML源代码时去钓鱼

What are you going to do with those image paths in your javascript? 您将如何处理javascript中的这些图片路径? If the final goal is to use them as the source of an img tag 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. 如果最终目标是将它们用作img标签的来源,那么您完全可以采取任何措施来隐藏它们,因为Firebug之类的工具将直接显示浏览器执行的所有HTTP请求,因此甚至不必查看HTML页面的源代码以获取图像路径。

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. 例如,您在javascript中生成一对私钥/公钥,并使用ajax将公钥发送到服务器脚本。 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. 该脚本使用此公共密钥对图像路径进行加密,并将其作为JSON数组返回给客户端脚本,该客户端脚本使用其私钥对其进行解密。


UPDATE: 更新:

Here's one example of sending the list of paths through AJAX: 这是通过AJAX发送路径列表的一个示例:

<?php
    header('Content-Type: application/json; charset=utf-8');
    $images_arr = array('some/dir/001.jpg', 'some/dir/002.jpg', 'some/dir/003.jpg');
    echo json_encode($images_arr);
?>

and you obtain them in javascript: 然后用javascript获取它们:

$(function() {
    $.getJSON('/script.php', function(data) {
        for (var x = 0; x < length; x += 1) {
            var imageUrl = data[x];
            // do something with this image url
        }
    });
});

I'm sorry to tell you but you have no choice. 很抱歉告诉您,但您别无选择。 If you want JavaScript to use your array, you have to pass it out to client and that means power user will be able to see it. 如果您希望JavaScript使用数组,则必须将其传递给客户端,这意味着高级用户将能够看到它。 There is no way around. 没有办法。

You can encode it but it won't hold against JavaScript debugger. 您可以对其进行编码,但是它不适用于JavaScript调试器。

If it is visible to JavaScript code, it is visible to user, period. 如果对JavaScript代码可见,则对用户,时期可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM