2

I'm trying to figure out I how can do something like:

console.log('<?php print_r($_SESSION); ?>'); 

To see the results in the console.

console.log('<?php echo serialize($_SESSION); ?>');

does not work either. Is there a way for me to echo the session information in firebug or inspect element in chrome for testing purposes?

Jason
  • 15,017
  • 23
  • 85
  • 116
Patrioticcow
  • 26,422
  • 75
  • 217
  • 337
  • 2
    are you using this code between script tags? – ahmet2106 Dec 29 '11 at 22:26
  • 1
    Not working does not qualify as a correct description about the error you face. You can not just throw anything out as javascript "string" and then expect it to work. Imagine there is a `'` inside the serialized string, it would just make it stop working. – hakre Dec 29 '11 at 22:33

2 Answers2

7

You could try this :

<script>
    console.log(<?php echo json_encode($_SESSION, JSON_HEX_TAG); ?>);
</script>

No quotes are required. See as well Firebug and Logging.


[edit, May 2014] Updated the code to be safe against XSS attacks. Always pass JSON_HEX_TAG to json_encode if you're embedding in HTML, or an attacker could inject code into the DOM by having you encode a string like:

</script><script>alert('Hello!');
Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
2

It is not possible to view the session details using fire bug.

animuson
  • 53,861
  • 28
  • 137
  • 147
Vijayan
  • 1,015
  • 2
  • 9
  • 12
  • 1
    Actually this answer is pretty correct by its nature and doesn't deserve to be downvoted, since it is really not possible to view the session data using only firebug - your serverside should prepare the data. And in this case the question makes no sense, because you can prepare anything: data from session, db, 3rd party service, etc – zerkms Dec 29 '11 at 22:36
  • In Chrome it's easily accessible under the Application tab. Is this still missing from Firebug? I understand that I could view it on server side, but it'd be easier to see it like in Chrome. – Csaba Toth Sep 05 '17 at 23:43