p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Arrays as Session Variables


Message #1 by "Bob Dolan" <rdolan@n...> on Mon, 16 Sep 2002 23:34:41
I have identified my "invitee" of interest in the database and have 
transferred that information to an array variable with the following code:

$invitee = mysql_fetch_row($result);

I want to keep the "invitee" record available throughout the session.  I'm 
starting with:

session_start();

but when it comes to registering the session variables, I don't know what 
the syntax is.  Options:

1) session_register(invitee); *or* session_register("invitee");

2) session_register(array("invitee[0]","invitee[1]"));

or how's about
3) session_register(array("invitee"));

I know I can do this the hard way (element by element), but I'd really 
like to know if there are some more elegant choices.

TIA

Message #2 by "Nikolai Devereaux" <yomama@u...> on Mon, 16 Sep 2002 16:00:37 -0700
$_SESSION['invitee'] = $invitee;


Session management becomes MUCH easier (imho) when you don't use
session_register, and interface with session variables directly through the
$_SESSION array.


If you decide to go the session_register route, then this is the correct
syntax:

session_register('invitee');


Let's go through your other suggestions so you can learn what's wrong with
them.  It'll give you a bit more insight into what's really going on and
help you code better in the future:

> 1) session_register(invitee);

The PHP parser sees an unrecognized identifier, invitee, and assumes it's a
defined constant.  (Unrecognized here means "not a built in function or
reserved word).  Since it doesn't find 'invitee' in the list of defined
constants, it should issue a warning and treat the token as the string
"invitee".  Note that you'll only see the warning if your error_reporting
level in PHP.ini has that level enabled.

> 2) session_register(array("invitee[0]","invitee[1]"));

This doesn't work because session_register accepts the NAMES of the
variables to be registered as session variables.  You're passing the VALUES
you'd like to persist.


> or how's about
> 3) session_register(array("invitee"));

This works, only because session register can accept an array containing the
names of variables to be registered.  Since there's only one variable,
though, (invitee), it's less efficient to force PHP to iterate through an
array to extract just the one name.


hope this helps,

nik

Message #3 by "Bob Dolan" <rdolan@n...> on Tue, 17 Sep 2002 00:43:32
nik suggested I use
> 
$_SESSION['invitee'] = $invitee;

I love it!  

On the other side (the next page) do I do the inverse?

$invitee = $_SESSION['invitee'];

or will the $invitee array emerge intact?

regards,  Bob



Message #4 by "Bob Dolan" <rdolan@n...> on Tue, 17 Sep 2002 00:52:48
> 
yeah, cool, 
Is there the possibility that I don't need to redefine the session 
variables on the other side, and can simply use the $_SESSION array?  Is 
that an option?

BTW: I really appreciate the full analysis of the choices I listed.  I 
have been coding since the '70s, but syntax in new environmanets is always 
a challenge.

Thanks,
Bob
Message #5 by "Nikolai Devereaux" <yomama@u...> on Mon, 16 Sep 2002 16:58:02 -0700
> nik suggested I use
> >
> $_SESSION['invitee'] = $invitee;
>
> I love it!
>
> On the other side (the next page) do I do the inverse?
>
> $invitee = $_SESSION['invitee'];
>
> or will the $invitee array emerge intact?

  ...

> Is there the possibility that I don't need to redefine the session
> variables on the other side, and can simply use the $_SESSION array?  Is
> that an option?
>
> BTW: I really appreciate the full analysis of the choices I listed.  I
> have been coding since the '70s, but syntax in new environmanets
> is always
> a challenge.

Assigning the value of $_SESSION['invitee'] to $invitee makes a COPY of
$_SESSION['invitee'].  That means that you introduce the possibility of
error -- your data might not be synchronized.

You can create a global reference to the $_SESSION var, like this:

$invitee = & $_SESSION['invitee'];

$invitee is now essentially just an alias into the original array stored at
$_SESSION['invitee'].  That way, any changes to $invitee directly modify
$_SESSION['invitee'].

I prefer to access all the values directly.  That is, don't create any
copies or references -- always access $_SESSION['invitee'] directly.


Hope this helps,

nik

p.s.  Here's a simple test script so you can see what's going on:

<?php

function printr($var, $desc = '')
{
  echo "<PRE>";
  if($desc != '')
    echo "$desc: ";
  print_r($var);
  echo "</PRE>\n";
}

session_start();

$invitee = $_SESSION['invitee'];

printr($_SESSION['invitee'], "\$_SESSION['invitee'], before");
printr($invitee, '$invitee, before');

$invitee['new_index'] = 'Nikolai was here!!';

printr($_SESSION['invitee'], "\$_SESSION['invitee'], after");
printr($invitee, '$invitee, after');

?>


  Return to Index