Looping through session variables

J

jwcarlton

Using PHP::Session, I have a variable like this:

my $session = PHP::Session->new($sid);

print "Content-type: text/html\n\n";
print "Favcolor : " . $session->get('favcolor');
exit;


Can you guys advise me on how to loop through all $session->get()
keys? I've tried everything I can think of, but since $session is not
an actual array, I'm not sure how to effectively loop through it.

FWIW, if I just print $session, it returns somethings like this:

PHP::Session=HASH(0x8c25de0)

TIA,

Jason
 
J

Jürgen Exner

jwcarlton said:
Can you guys advise me on how to loop through all $session->get()
keys? I've tried everything I can think of, but since $session is not
an actual array, I'm not sure how to effectively loop through it.

FWIW, if I just print $session, it returns somethings like this:
PHP::Session=HASH(0x8c25de0)

Great! That means $session is a reference to a hash.
All you have to do is to derefence that reference(*) and then use the
keys() function to get the keys of that hash.
However, I am pretty sure that this module provides methods or functions
to retrieve those values. You should use them instead of rolling your
own code because it is a very bad idea to take advantage of
implementation interna of a module. Those can change at any time and
then you are screwed.

*: how to create a reference and how to dereference it again is
explained in "perldoc perlref". There is also a rather good tutorial
available in "perdoc perlreftut".

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top