Matt
Arrays in Session?????
Moderator: General Moderators
-
matthiasone
- Forum Contributor
- Posts: 117
- Joined: Mon Jul 22, 2002 12:14 pm
- Location: Texas, USA
- Contact:
Arrays in Session?????
Does anyone know if you can store arrays in sessions?
Matt
Matt
- Heavy
- Forum Contributor
- Posts: 478
- Joined: Sun Sep 22, 2002 7:36 am
- Location: Viksjöfors, Hälsingland, Sweden
- Contact:
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.
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
Code: Select all
<?php
$array = array( 'item_1' => 'soap', 'item_2' => 'tissue', 'item_3' => 'wax' );
$_SESSIONї'items'] = $array;
echo $_SESSIONї'items']ї'item_1'];
?>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
-
matthiasone
- Forum Contributor
- Posts: 117
- Joined: Mon Jul 22, 2002 12:14 pm
- Location: Texas, USA
- Contact: