Re: [PHP] SESSION array problems [SOLVED]



On Thu, Oct 2, 2008 at 11:02 AM, tedd <tedd.sperling@xxxxxxxxx> wrote:
Hi gang:

As strange as it may seem, but when session variables are passed to another
page (i.e., used) you cannot extract ALL OF THEM using a loop when the
variable names you are using are the same as the SESSION index's names.

In other words, you cannot do this:

for ($i = 0; $i < $num_users; $i++)
{
$last_name = $_SESSION['last_name'][$i];
$first_name = $_SESSION['first_name'][$i];
echo("<p>$last_name $first_name</p>");
}

But you can do this:

for ($i = 0; $i < $num_users; $i++)
{
$last = $_SESSION['last_name'][$i];
$first = $_SESSION['first_name'][$i];
echo("<p>$last $first</p>");
}

See the difference?

This was a crazy one.

Now, someone show me where that is documented?

Cheers,

tedd


As several of us have suggested now, it's got to be register_globals.
That would make the following blocks of code equivalent:

<?php

for ($i = 0; $i < $num_users; $i++)
{
$last_name = $_SESSION['last_name'][$i];
$first_name = $_SESSION['first_name'][$i];
echo("<p>$last_name $first_name</p>");
}

for ($i = 0; $i < $num_users; $i++)
{
$_SESSION['last_name'] = $_SESSION['last_name'][$i];
$_SESSION['first_name'] = $_SESSION['first_name'][$i];
echo("<p>{$_SESSION['last_name']} {$_SESSION['first_name']}</p>");
}

?>

Andrew
.



Relevant Pages

  • Re: [PHP] SESSION array problems [SOLVED]
    ... As strange as it may seem, but when session variables are passed to another page you cannot extract ALL OF THEM using a loop when the variable names you are using are the same as the SESSION index's names. ...
    (php.general)
  • Re: [PHP] SESSION array problems [SOLVED]
    ... As strange as it may seem, but when session variables are passed to another ... you cannot extract ALL OF THEM using a loop when the ... More full-root dedicated server packages: ...
    (php.general)
  • Re: [PHP] SESSION array problems [SOLVED]
    ... tedd wrote: ... As strange as it may seem, but when session variables are passed to ... you cannot extract ALL OF THEM using a loop ...
    (php.general)
  • Re: [PHP] Re: SESSION array problems UPDATE
    ... step which shows a list of the SESSION variables in both the top left ... the next portion of the code is the foreach loop where the ... when it hit's the second iteration on the for loop it now has strings to deal with so uses the $key as a string offset thus giving you the second character of the string variables $last/$first. ...
    (php.general)
  • Re: Session.Contents.Remove
    ... The For Each loop however doesn't seem to be adjusting ... misses a few items because the collection reorderns itself. ... In the second loop I run over my array, ... ALL of the session variables were even being tested. ...
    (microsoft.public.inetserver.asp.general)