How to read SESSION variables with JQUERY

11,890

Solution 1

You can't access session variables directly from jQuery: Sessions run on the server; JavaScript runs on the client.

You'd have to export the variables from PHP to JavaScript, e.g. like so:

<script>
SESSION = { 
 "abc": "<?php echo $_SESSION["abc"]; ?>",
 "def": "<?php echo $_SESSION["def"]; ?>",
 "ghi": "<?php echo $_SESSION["ghi"]; ?>",
 };

alert(SESSION.abc);  // will output "abc"

</script>

Be careful to export only session variables that are not security relevant.

Solution 2

Short answer:

You cannot read SESSION directly from jQuery

Share:
11,890
KillerFish
Author by

KillerFish

Updated on July 21, 2022

Comments

  • KillerFish
    KillerFish almost 2 years

    I am having requirement in my project that I need to READ A SESSION VARIABLE WITH JQUERY and the session variable is created using PHP. How can I achieve this?