将JS文件解析为PHP的副作用是什么


What are the side effects of parsing JS files as PHP?

我有一些php变量需要在javascript中使用。我没有使用ajax来回传递vars,而是选择将js文件解析为php。下面是我所做的一个简单的例子:

#.htaccess
AddType application/x-httpd-php .js
//scripts.js
//or for security reason, I could just use scripts.php and add header at the top)
header("Content-type: text/javascript");
alert("Hello <?php echo $_SESSION['username']; ?>");

我用这种方法已经有一段时间了,没有发现任何明显的问题。

这样做有什么副作用吗?谢谢

我看不出有任何问题,但对我来说,一个更简单的解决方案是:

<script type="text/javascript">
   var username = "<?=$_SESSION["username"]?>";
</script>

在模板或索引的头区域中。