Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access session variables from JavaScript

Is it possible to access session variables made in from JavaScript, and how can I replicate this PHP code in JavaScript?

if(empty($_SESSION['name'] {
   echo 'foo';
}
like image 427
H Bellamy Avatar asked Dec 17 '22 07:12

H Bellamy


1 Answers

You could access a page via Ajax which would return the variable value:

Using jQuery:

$.get('myVariable.php', function ( data ) {
    alert(data)
});

And the PHP page (myVariable.php):

<?php echo $_SESSION['user_id']; ?>
like image 143
Eliseu Monar dos Santos Avatar answered Jan 07 '23 10:01

Eliseu Monar dos Santos