Looping through session variables

  • Follow


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
0
Reply jwcarlton 12/23/2010 3:09:42 AM

jwcarlton <jwcarlton@gmail.com> wrote:
>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
0
Reply J 12/23/2010 3:56:13 AM


jwcarlton <jwcarlton@gmail.com> writes:

> 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?

The module doesn't provide an API for it, but if you're willing to dig
into the $session object's internals, you can access its _data member
variable directly. It's a hashref, so:

  foreach my $key (keys %{$session->{_data}}) {
    # Do stuff with $key
  }

sherm--

-- 
Sherm Pendley
                                   <http://camelbones.sourceforge.net>
Cocoa Developer
0
Reply Sherm 12/23/2010 4:01:25 AM

Jürgen Exner <jurgenex@hotmail.com> writes:

> 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.

Agree 100%, but it turns out that PHP::Session only provides a simple
one-at-a-time get() method:

  sub get {
      my($self, $key) = @_;
      return $self->{_data}->{$key};
  }

Sometimes, an ugly hack is the only option... :-(

sherm--

-- 
Sherm Pendley
                                   <http://camelbones.sourceforge.net>
Cocoa Developer
0
Reply Sherm 12/23/2010 4:12:24 AM

3 Replies
444 Views

(page loaded in 0.078 seconds)


Reply: