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
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Arrays in Session?????

Post by matthiasone »

Does anyone know if you can store arrays in sessions?

Matt :?:
redptam
Forum Newbie
Posts: 8
Joined: Thu Oct 17, 2002 3:25 pm
Location: Atlanta, GA

Post by redptam »

This tutorial shows how to do this here:
http://www.phpcomplete.com/content.php?id=14

Good luck!

:lol:
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post 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>
SuperHuman
Forum Newbie
Posts: 10
Joined: Wed Oct 16, 2002 10:00 pm

Post 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.....
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
matthiasone
Forum Contributor
Posts: 117
Joined: Mon Jul 22, 2002 12:14 pm
Location: Texas, USA
Contact:

Post by matthiasone »

thanks for the input. For some reason I couln't bend my mind around that concept.

Matt
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

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