相同的 Javascript 代码,具有 PHP 的独特输出

Same Javascript code, unique outputs with PHP

我有以下 Javascript:

<script>
function myFunction() { 
    document.write("Hello Whosebug users!");
}
myFunction();
</script>

是否有任何 recommended/fast 使用 PHP encode/encrypt/minify/pack JavaScript 的方法,所以每次输出都不同(使用随机字符串打包 js 或类似的东西)?
我只想要相同的功能,每次都会做同样的事情,但每次都使用不同的 JavaScript 代码。

在 PHP

中创建随机字符串函数
<?php
/*
 * Create a random string
 * @author  XEWeb <>
 * @param $length the length of the string to create
 * @return $str the string
 */
function randomString($length = 6) {
    $str = "";
    $characters = array_merge(range('A','Z'), range('a','z'), range('0','9'));
    $max = count($characters) - 1;
    for ($i = 0; $i < $length; $i++) {
        $rand = mt_rand(0, $max);
        $str .= $characters[$rand];
    }
    return $str;
}
?>

然后修改你的js函数如下

 <script>
    //assign php variable to js variable
    function myFunction() { 
        var randomString=<?php echo randomString(10);?>
        document.write(randomString);
    }
    myFunction();
 </script>

这将允许您在每次调用 JS 函数时写入随机字符串

[PHP 函数取自 'https://www.xeweb.net/2011/02/11/generate-a-random-string-a-z-0-9-in-php/']

由于大多数 packers/compressors 总是使用没有任何种子的相同算法,您可以尝试在函数本身前后添加一些随机的 js 垃圾。

<?php
function getRandomGarbage(){
  return "\nfunction " . uniqid() . "(){}\n";
}
$myJsFunction = "... put your js here ";
//You can send the following to a php js compressor or pack it yourself
echo getRandomGarbage() . $myJsFunction . getRandomGarbage();