从Jquery获取PHP $ _SESSION变量? [英] Get a PHP $_SESSION variable from Jquery?
本文介绍了从Jquery获取PHP $ _SESSION变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我还不是JSON / AJAX大师,所以我不知道怎么做。
I'm not yet a JSON/AJAX master so I don't know how to do this.
我需要一个$ _SESSION ['name'] PHP变量与我的jQuery东西一起工作,我不知道如何访问它...考虑:
I need a $_SESSION['name'] PHP variable to work with in my jQuery stuff and I don't know how to access it... consider:
// the 'who is typing' shindig
$.ajax(
{
url: "whos_typing.html",
cache: false,
success: function(whos)
{
// here I need to access $_SESSION['name'] and do stuff with it
$("#soandso").html(whos); //Insert who's typing into the #soandso
}
});
推荐答案
你需要注射它,像这样的东西:
You'll need to inject it, something like this:
var sessName = '<?php echo $_SESSION['name']?>';
包含此脚本的文件必须由php解释器执行(即.php文件)
The file containing this script must be executed by the php interpreter (i.e. a .php file)
编辑:承认Radu的观点,对未经过抽样的数据执行会更安全:
Conceding to Radu's point, it would be safer execute for unsanitized data:
var sessName = <?php echo json_encode($_SESSION['name']) ?>;
这篇关于从Jquery获取PHP $ _SESSION变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文