Page 1 of 1

Arrays in Session?????

Posted: Fri Oct 18, 2002 12:01 pm
by matthiasone
Does anyone know if you can store arrays in sessions?

Matt :?:

Posted: Fri Oct 18, 2002 12:09 pm
by redptam
This tutorial shows how to do this here:
http://www.phpcomplete.com/content.php?id=14

Good luck!

:lol:

Posted: Fri Oct 18, 2002 1:19 pm
by Heavy
I thought I'd give it some time:
It looks a little cluttery, but it is quite a proof that $_SESSION can take arrays.
You might want to use "session_register()" instead, but I think that "$_SESSION" is better because of both security and code readability reasons.

Code: Select all

<?
session_start();
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
<HTML>
<HEAD>
</HEAD>
<BODY>

  <H5>I use PHP 4.2.3.<br>
  Note: $_SESSION array was Introduced in 4.1.0. In earlier versions, use $HTTP_SESSION_VARS.<br>
  I have myself at some time had problems when using the <I>$_SESSION</I> array and <I>session_register()</I> in the same php-script.<br>You might not want to do that...
  <br><br>Press reload a couple of times and see what happens.
  </H4>
  <?
    echo "<a href="{$_SERVERї'SCRIPT_NAME']}">Reload</A><br>";
    if ($_GETї'CLEAR']!="YES"){
        echo "<a href="{$_SERVERї'SCRIPT_NAME']}?CLEAR=YES">Clear the session</A>";
    }

  echo "<br><br>";


  if ($_GETї'CLEAR']=="YES"){
    unset($_SESSIONї'Array']);
  }



  $_SESSIONї'Array']ї$a++]="Superman";
  $_SESSIONї'Array']ї$a++]="is";
  $_SESSIONї'Array']ї$a++]="made";
  $_SESSIONї'Array']ї$a++]="of";
  $_SESSIONї'Array']ї$a++]="jelly.";

  foreach ($_SESSIONї'Array'] as $Item){
           if (is_array($Item)){
               foreach($Item as $Sub_Item){
                       echo " $Sub_Item";
               }
           }else{
               echo " $Item";
           }
  }

  echo "<br><br><br>";

  // another way of adding data:

  $_SESSIONї'Array']ї]="<br>";
  $_SESSIONї'Array']ї]="Still";
  $_SESSIONї'Array']ї]=", ";
  $_SESSIONї'Array']ї]="he";
  $_SESSIONї'Array']ї]="hits";
  $_SESSIONї'Array']ї]="like";
  $_SESSIONї'Array']ї]="nuclear";
  $_SESSIONї'Array']ї]="eggz.";

  // a three dimensional array example:
  $_SESSIONї'Array']ї0]ї]= $_SESSIONї'Array']ї0]їcount($_SESSIONї'Array']ї0])-1]+1;

  foreach ($_SESSIONї'Array'] as $Item){
           if (is_array($Item)){
               foreach($Item as $Sub_Item){
                       echo " $Sub_Item";
               }
           }else{
               echo " $Item";
           }
  }

?>
</BODY>
</HTML>

Posted: Fri Oct 18, 2002 4:03 pm
by SuperHuman

Code: Select all

<?php


$array = array( 'item_1' => 'soap', 'item_2' => 'tissue', 'item_3' => 'wax' );
$_SESSIONї'items'] = $array;

echo $_SESSIONї'items']ї'item_1'];


?>
In other words, create your array and register it inside the $_SESSION variable. This will create a 2-d array which can be accessed as above in the echo statement - adding arrays to the $_SESSION Array is as simple as baking a cake in PHP.....

Posted: Mon Oct 21, 2002 4:40 am
by twigletmac
Heavy - You don't want to be using session_register() unless you are using PHP 4.0.6 or below, it doesn't work with register_globals off and cannot be used in conjunction with $_SESSION or $HTTP_SESSION_VARS.

Mac

Posted: Mon Oct 21, 2002 9:51 am
by matthiasone
thanks for the input. For some reason I couln't bend my mind around that concept.

Matt

Posted: Mon Oct 21, 2002 12:40 pm
by Heavy
twigletmac wrote:Heavy - You don't want to be using session_register() unless you are using PHP 4.0.6 or below...
Thanks!
I didn't know that.