Read session value with Javascript


Question

Is it possible to read a session value with Javascript?

For example, if I assigned a value into a session in PHP:

$_SESSION['msg'] = "ABC Message";

Is it possible to read $_SESSION['msg'] with Javascript?

1
14
4/16/2009 4:39:23 AM

Accepted Answer

$_SESSION is a server-side construct. You would need to store that variable in $_COOKIE to be able to access it client-side.

20
4/16/2009 4:36:41 AM

A very simple way is to generate the JavaScript with some PHP code:

<script type="text/javascript">
    <?php echo 'var msg = "'.json_encode($_SESSION['msg']).'";';
</script>

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon