javascript 如何在javascript中访问php $_SESSION数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17270962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reach php $_SESSION array in javascript?
提问by Basel
I am trying to build some javascript function and I need to check if the users are logged in or not. When a user log in to my site I set a variable in session array named is_logged. I want to reach that variable in javascript is it possible??? I tried some ways but did not work like following:
我正在尝试构建一些 javascript 函数,我需要检查用户是否已登录。当用户登录我的站点时,我在名为 is_logged 的会话数组中设置了一个变量。我想在 javascript 中达到那个变量有可能吗???我尝试了一些方法,但没有像以下那样工作:
var session = "<?php print_r $_SESSION['is_logged']; ?>";
alert(session);
and:
和:
var session = '<?php echo json_encode($_SESSION['is_logged']) ?>';
alert(session);
It is either show a text or never alert at all
它要么显示文本,要么根本不提醒
采纳答案by Kemal Da?
If you want to reach all elements of $_SESSION in JavaScript you may use json_encode,
如果你想在 JavaScript 中访问 $_SESSION 的所有元素,你可以使用 json_encode,
<?php
session_start();
$_SESSION["x"]="y";
?>
<script>
var session = eval('(<?php echo json_encode($_SESSION)?>)');
console.log(session);
//you may access session variable "x" as follows
alert(session.x);
</script>
But note that, exporting all $_SESSION variable to client is not safe at all.
但请注意,将所有 $_SESSION 变量导出到客户端根本不安全。
回答by Voitcus
Just echo it:
只需回声:
var session = <?php echo $_SESSION['is_logged']?'true':'false'; ?>;
alert(session);
You need tertiary operator, as false
is echoed as empty string so it would lead to var session = ;
which is a JS syntax error.
您需要三级运算符,因为false
它作为空字符串回显,因此会导致var session = ;
JS 语法错误。
回答by Nesmar Patubo
You can do things like this
你可以做这样的事情
Just insert the $_SESSION['is_logged']
in a hidden field like
只需$_SESSION['is_logged']
在隐藏字段中插入
<input type = "hidden" value = "<?php echo $_SESSION['is_logged']; ?>" id = "is_logged" />
Then you can access that in your jquery like this
然后你可以像这样在你的 jquery 中访问它
var is_logged = jQuery.trim($('#is_logged').val());
//then do validation here
回答by Deepu
In a js file you cant get the value of a php variable or php code doesnt works on a js file because php code will works in a .php extension file. So one method is to set the session value as a hidden element value and in your js file get the value of the hidden element.
在 js 文件中,您无法获得 php 变量的值,或者 php 代码不适用于 js 文件,因为 php 代码将在 .php 扩展文件中工作。所以一种方法是将会话值设置为隐藏元素值,并在您的 js 文件中获取隐藏元素的值。
In the html:
在 html 中:
<input type="hidden" id="sess_var" value="<?php echo $_SESSION['is_logged']; ?>"/>
In your js:
在你的js中:
var session = document.getElementById('sess_var').value;
alert(session);
回答by Martin Lantzsch
Try the following code:
试试下面的代码:
var session = "<?php echo $_SESSION['is_logged'] ?>";
回答by Aas Mohammd
var get_session=<?php echo $_SESSION['is_login']
alert(get_session);
?>
## *Just try this *##
## *试试这个 *##