开发者

Get session using javascript

开发者 https://www.devze.com 2023-03-29 07:32 出处:网络
The cookies can get by javascript if we use document.javascript. I want to know : get session($_SESSION[\"session_var_name\"]) by javascript () ?开发者_StackOverflow中文版????You can\'t. The data sto

The cookies can get by javascript if we use document.javascript.

I want to know : get session($_SESSION["session_var_name"]) by javascript () ?开发者_StackOverflow中文版????


You can't. The data stored in the PHP $_SESSION super global is server-side only.

The cookies can get by javascript if we use document.javascript.

FYI, it's document.cookie


If you need any value from the $_SESSION array, you must pass it.

example:

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

you could also do it like this, BUT is really BAD practice:

<script type="text/javascript">
    // try this only at home :)
    var session = <?= json_encode($_SESSION) ?>;
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消