2016-06-10 75 views
1

独特的输出,我有以下的Javascript:相同的JavaScript代码,用PHP

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

是任何建议/快速的方式来编码/加密/缩小/使用PHP所以输出是不同的,每次收拾的JavaScript(使用随机字符串打包js或类似的东西)?
我只是想要相同的功能,每次都会做同样的事情,但每次都使用不同的JavaScript代码。

+0

用PHP随机化JS函数行为?使用'if else'切换不同的功能,可以做不同的事情 –

+0

@MeganFox该功能只是一个简化事情的例子。我有一个完整的js文件需要做与现在一样的事情,但代码略有不同。 – dracosu

+0

@dracosu你期待什么类型的输出,请给出提示你有一组消息数组,它应该随机返回 – user1234

回答

1

由于大多数压缩器/压缩器总是使用相同的算法而没有任何种子,因此您可能会尝试在函数本身之前和之后添加一些随机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(); 
+0

感谢您的提示!但是,顺便说一句,我期待着为整个js文件制作独特的片段,而不仅仅是一个功能。然而,从你的建议开始,我认为我可以做到! – dracosu

-1

在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调用函数时

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

+0

你好Projesh,我正在寻找一个独特的代码片段,不仅是一个独特的字符串,所以你的解决方案不适合这个问题。 – dracosu