David coder
David coder

Reputation: 17

Get session using javascript

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

I want to know : get session($_SESSION["session_var_name"]) by javascript () ?????

Upvotes: 0

Views: 6298

Answers (2)

Teneff
Teneff

Reputation: 32148

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>

Upvotes: 2

Phil
Phil

Reputation: 164730

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

Upvotes: 6

Related Questions