haku Posted February 25, 2008 Share Posted February 25, 2008 Does anybody know a way to output all the variable names in a session as well as their values, and if so, can you tell me? Thank you very much. Link to comment https://forums.phpfreaks.com/topic/92852-output-session-variables/ Share on other sites More sharing options...
affordit Posted February 25, 2008 Share Posted February 25, 2008 echo "Welcome: ". $_SESSION['username']; Link to comment https://forums.phpfreaks.com/topic/92852-output-session-variables/#findComment-475629 Share on other sites More sharing options...
haku Posted February 25, 2008 Author Share Posted February 25, 2008 Thanks, but I mean I want a list of all the variables that are set in the current session. Link to comment https://forums.phpfreaks.com/topic/92852-output-session-variables/#findComment-475642 Share on other sites More sharing options...
mmarif4u Posted February 25, 2008 Share Posted February 25, 2008 If i am getting u right: <pre> <?print_r($_SESSION);?> </pre> Link to comment https://forums.phpfreaks.com/topic/92852-output-session-variables/#findComment-475644 Share on other sites More sharing options...
haku Posted February 25, 2008 Author Share Posted February 25, 2008 I think you were getting me right! (although I think you had a syntax error there ) Thanks! Link to comment https://forums.phpfreaks.com/topic/92852-output-session-variables/#findComment-475657 Share on other sites More sharing options...
aschk Posted February 25, 2008 Share Posted February 25, 2008 Actually what you're really after is the <?php $keys = array_keys($_SESSION); foreach($keys as $k){ echo $k; } ?> The above will fetch all the key names of your associative array (which is what the session is essentially). print_r has it's uses (for debugging) but you can't use the key names with it Link to comment https://forums.phpfreaks.com/topic/92852-output-session-variables/#findComment-475661 Share on other sites More sharing options...
haku Posted February 26, 2008 Author Share Posted February 26, 2008 Thank you aschk, that helped a lot. print_r unfortunately didn't work the way I wanted. For anyone who may see this thread in the future, I ended up using this code (a slight variation on aschk's): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "XHTML1-s.dtd" > <html> <head> <title>Session Variable List</title> <style type="text/css"> table { border: 3px solid #000000; border-collapse: collapse; } td { border: 1px solid #000000; } </style> </head> <body> <table> <thead> <tr> <td>Variable Name</td> <td>Value</td> </tr> </thead> <tbody> <?php session_start(); $keys = array_keys($_SESSION); foreach($keys as $k) { echo "<tr><td>" . $k . "</td><td> " . $_SESSION[$k] . "</td></tr>"; } ?> </tbody> </table> </body> </html> This outputs all the session variables names and their values in a nice little easy to read table. Link to comment https://forums.phpfreaks.com/topic/92852-output-session-variables/#findComment-476720 Share on other sites More sharing options...
trq Posted February 26, 2008 Share Posted February 26, 2008 Could also be simply achieved using.... <?php foreach($_SESSION as $k as $v) { echo "<tr><td>" . $k . "</td><td> " . $_SESSION[$k] . "</td></tr>"; } ?> Link to comment https://forums.phpfreaks.com/topic/92852-output-session-variables/#findComment-476722 Share on other sites More sharing options...
Jessica Posted February 26, 2008 Share Posted February 26, 2008 thorpe, don't you mean this? foreach($_SESSION as $k=>$v) { Link to comment https://forums.phpfreaks.com/topic/92852-output-session-variables/#findComment-476738 Share on other sites More sharing options...
trq Posted February 26, 2008 Share Posted February 26, 2008 Indeed I did, shows what you get when you rush things. Link to comment https://forums.phpfreaks.com/topic/92852-output-session-variables/#findComment-476750 Share on other sites More sharing options...
haku Posted February 26, 2008 Author Share Posted February 26, 2008 I have my own mini library of scripts like that. I use them for testing purposes - I drop them onto my server and delete them after use. I like to make them valid just cause Im an@l that way. The core of what I was doing was what Thorpe showed, but I prefer the full deal for myself. Link to comment https://forums.phpfreaks.com/topic/92852-output-session-variables/#findComment-476807 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.