arrays in $_SESSION

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
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

arrays in $_SESSION

Post by Bill H »

Here's my plan. I want to open a data file and load the data when the user logs on, then update it when he clicks an "Update" button. In between I want him to add/modify/delete elements of an array which are not recorded to the data file until he clicks the "Update" button and are discarded if he leaves without clicking it. But that discard needs to persist if he returns during the same browser session. I have all of it working except for the array.

Here's how I load the array:

Code: Select all

<?php
unset($_SESSIONї'names']);              
// make sure nothing left over from previous login during this session

$r = "";
$f = @fopen("files/acode.dat", "r");          // open names/codes file
if ($f != FALSE)
{    $a = @fread($f, filesize("files/acode.dat"));
       fclose($f);
       $r = trim($a);                           // trim any excess
       $adata = explode("|", $r);                 // array of names
       for ($i = 0; $i < count($adata); $i++)
       {    $_SESSIONї'namesї$i]'] = $adataї$i];
       }
}
?>
I printed out the array of names ($adata) and it is complete with all the names. But when I print_r($_SESSION['name']) i get nothing.

During the session I add to the array as follows:

Code: Select all

<?php
$txt1 = $_POSTї'NewNam'];
if (strlen($txt1)  == 4)
{    $_SESSIONї'namesї]'] = $txt1;
}
// it was not added, so I tried
$txt1 = $_POSTї'NewNam'];
if (strlen($txt1)  == 4)
{    $i = 0;
       while(isset($_SESSIONї'namesї$i]']))
            $i++;
       $_SESSIONї'namesї$i]'] = $txt1;
}
// but it timed out on the "while" line
?>
It looks like there is something I fundamentally do not understand about arrays in $_SESSION. Can anyone point me in the right direction here?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try $_SESSION['names'][$i] instead of $_SESSION['names[$i]']
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Thanks. Your reply indicates that I was correct in the following statement.
there is something I fundamentally do not understand about arrays in $_SESSION
However, the following:

Code: Select all

<?php
$adata = explode("|", $dї0]);                 // array of names
for ($i = 0; $i < count($adata); $i++)
{    $_SESSIONї'names']ї$i] = $adataї$i];
}
?>
Still leaves no list of names in $_SESSION, and this:

Code: Select all

<?php
if (strlen($txt1) == 4)
{    $_SESSIONї'names']ї] = $txt1;
}
?>
does not add an element.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Do you have session_start() at the top of the script?

Mac
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Yes, I do. The other S_SESSION variables, ones that aren't arrays, work as expected.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

This works. [Using strlen() instead of isset().]
Don't know why, but....
Volka, your help was immensly valuable.

Code: Select all

<?php
          if (strlen($txt1) && strlen($txt2) == 4)
          {    $i = 0;
               while(strlen($_SESSIONї'names']ї$i])) $i++;
               $_SESSIONї'names']ї$i] = $txt1;
               $_SESSIONї'codes']ї$i] = $txt2;
          }
?>
Post Reply