Session variables, How can i get all the defined keys?

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
Roo
Forum Newbie
Posts: 2
Joined: Tue Oct 07, 2003 7:36 pm

Session variables, How can i get all the defined keys?

Post by Roo »

I'm wanting to put a session object though a "for each loop". all i really want is the name and value of all the keys in a session. Can any body help me.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Have you tried:

Code: Select all

<?php
	session_start();

	$_SESSION['user'] = "nigma";
	$_SESSION['blah'] = "blah";

	foreach ($_SESSION as $sessValue)
	{
		print $sessValue;
	}

?>
This worked for me, let me know if it does for you?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

To expand on nigma's answer:

Code: Select all

foreach ($_SESSION as $key => $value) {
    echo 'key = '.$key.' and value = '.$value.'<br />';
}
Mac
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Oops, sorry, I read to quickly. You probably already knew how to do what I replied with.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

array_keys and array_values should be of interest as well.
Roo
Forum Newbie
Posts: 2
Joined: Tue Oct 07, 2003 7:36 pm

Post by Roo »

Thanks for all your help guys every thing worked fine.

your all Champs
Gog bless

Roo
Post Reply