Listing Every Session Variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Listing Every Session Variable

Post by tecktalkcm0391 »

Is there a way to make PHP print:

$_SESSION['name'] = hi
$_SESSION['name2'] = asdf
$_SESSION['name3'] = hh

which is pretty much just it finding all of the sessions variables and then printing it. What I just want is a list of all of the variables on the page, so that I can be lazy an not have to find them all
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Code: Select all

<?php

	print_r($_SESSION);

?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

print_r($_SESSION);

// or if your REALLY  lazy..
print_r($GLOBALS);
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

A number of programmers keep something like this around. I stick it in config.php and comment out the echo in production code to minimize stray print statements.

Code: Select all

function dump($var) {
     echo '<pre>' . print_r($var, 1) . '</pre>';
}
(#10850)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

arborint wrote:A number of programmers keep something like this around. I stick it in config.php and comment out the echo in production code to minimize stray print statements.

Code: Select all

function dump($var) {
     echo '<pre>' . print_r($var, 1) . '</pre>';
}
Yeah i do that.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

arborint wrote:A number of programmers keep something like this around. I stick it in config.php and comment out the echo in production code to minimize stray print statements.

Code: Select all

function dump($var) {
     echo '<pre>' . print_r($var, 1) . '</pre>';
}
What's wrong with:

Code: Select all

<?php

        print_r($_SESSION);

?>
Except to the presentation?
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

I prefer to use var_dump() myself...
It gives you alot more detailed info than print_r().

But thats just me :)
(i deal with alot of binary reverse engineering in PHP :P so i need to know true string lengths ect.)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

R4000 wrote:I prefer to use var_dump() myself...
It gives you alot more detailed info than print_r().

But thats just me :)
(i deal with alot of binary reverse engineering in PHP :P so i need to know true string lengths ect.)
This really depends on your needs... For me, most of the time the extra data from var_dump() is useless and confusing - Not because I don't understand it :P , but because this is an extra text to read even though I don't need this information (in most cases).
Post Reply