Storing php $_GET variable in a javascript variable?(将 php $_GET 变量存储在 javascript 变量中?)
问题描述
我正在使用 $_GET 方法(team1、team2)将两条信息传递给一个 php 页面.我想在一些 javascript 中使用这些作为变量.我该怎么做?
I am passing two pieces of info to a php page using the $_GET method (team1, team2). I'd like to use these as variables in some javascript. How can I do this?
谢谢
推荐答案
原答案:
在您的 .php 文件中.
In your .php file.
<script type="text/javascript">
var team1, team2;
team1 = <?php echo $_GET['team1']; ?>;
team1 = <?php echo $_GET['team1']; ?>;
</script>
更安全的答案:
当我爆出这个答案时,甚至没有考虑 XSS.(看评论!)$_GET 数组中的任何内容都应该转义,否则用户几乎可以将他们想要的任何 JS 插入到您的页面中.所以尝试这样的事情:
Didn't even think about XSS when I blasted this answer out. (Look at the comments!) Anything from the $_GET array should be escaped, otherwise a user can pretty much insert whatever JS they want into your page. So try something like this:
<script type="text/javascript">
var team1, team2;
team1 = <?php echo htmlencode(json_encode($_GET['team1'])); ?>;
team1 = <?php echo htmlencode(json_encode($_GET['team1'])); ?>;
</script>
从这里 http://www.bytetouch.com/blog/programming/protecting-php-scripts-from-cross-site-scripting-xss-attacks/.
来自 Google 的有关 XSS 的更多信息 http://code.google.com/p/doctype/wiki/ArticleXSSInJavaScript.
More about XSS from Google http://code.google.com/p/doctype/wiki/ArticleXSSInJavaScript.
为评论者干杯.
这篇关于将 php $_GET 变量存储在 javascript 变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 php $_GET 变量存储在 javascript 变量中?
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
