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?
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?
$_SESSION is a server-side construct. You would need to store that variable in $_COOKIE to be able to access it client-side.
.. Or you can use ajax to retrive your server side session value into you client-side javascript.` (quick, dirty and untested example, using jQuery)
Javascript Side:
$.ajax({
url: "test.php",
cache: false,
success: function(html){
eval( html ); /// UGLY NASTY YOU MUST VALIDATE YOUR INPUTS... JUST AN EXAMPLE
}
});
PHP side test.php:
echo 'var myData = "'. $_SESSION['msg'].'"';
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>