How to read SESSION OPTIONS using JQUERY

I have a requirement in my project that I need to READ THE VARIABLE SESSION WITH JQUERY , and the session variable is created using PHP. How can i achieve this?

thank

+3
source share
5 answers

You cannot access session variables directly from jQuery: sessions running on the server; JavaScript works on the client.

You will have to export variables from PHP to JavaScript, for example. 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 related.

+6
source

Short answer:

SESSION jQuery

+1

, javascript

JavaScript (usually) runs only on the client side, and PHP runs on the server side. You cannot directly access session data from javascript.

0
source
var id = <? echo $_SESSION['id'] ?>;    
0
source

Another method you can use is to enter a value from a session variable into a hidden text field, and then get jquery to read from that text field

0
source

Source: https://habr.com/ru/post/1782244/


All Articles