Arrays in Session?????
Posted: Fri Oct 18, 2002 12:01 pm
Does anyone know if you can store arrays in sessions?
Matt
Matt
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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>Code: Select all
<?php
$array = array( 'item_1' => 'soap', 'item_2' => 'tissue', 'item_3' => 'wax' );
$_SESSIONї'items'] = $array;
echo $_SESSIONї'items']ї'item_1'];
?>Thanks!twigletmac wrote:Heavy - You don't want to be using session_register() unless you are using PHP 4.0.6 or below...