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. Quote Link to comment Share on other sites More sharing options...
affordit Posted February 25, 2008 Share Posted February 25, 2008 echo "Welcome: ". $_SESSION['username']; Quote Link to comment 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. Quote Link to comment 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> Quote Link to comment 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! Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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>"; } ?> Quote Link to comment 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) { Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.