Unsetting Multi-Dimensional Array Session Variables

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
<br>
Forum Commoner
Posts: 35
Joined: Thu May 01, 2008 2:42 pm

Unsetting Multi-Dimensional Array Session Variables

Post by <br> »

I store a user's shopping cart in an array like this:

$_SESSION['cart'][0]['any_Attribute']
$_SESSION['cart'][1]['any_Attribute']

Here's how I destroy a cart item:

if ($_GET['action'] == 'delete'){
unset($_SESSION['cart'][$_GET['number']]);
$_SESSION['cart']=array_merge($_SESSION['cart']);
}

Will unset() destroy every attribute? Say there were 2 items... deleting
item "0" and merging the cart will mean that item "1" is now item "0"... but
have all of the deleted item's attributes been completely destroyed? Since
not all items have every attribute it won't always be over-written... It has
always seemed to function properly...

Thanks,
Jacob
<br>
Forum Commoner
Posts: 35
Joined: Thu May 01, 2008 2:42 pm

Re: Unsetting Multi-Dimensional Array Session Variables

Post by <br> »

Actually, now I see that every time I add something, I set every attribute as:

$_SESSION['cart'][$counter]['attribute']=$_POST['attribute'];

meaning that each attribute will be set to empty and overlap should be impossible...

I understand that POST variables are vulnerable to manipulation... if I use the POST
information as follows, doesn't it eliminate any threat from this? If the post doesn't
match, nothing happens...

Code: Select all

$query  = "SELECT * FROM items";
$result = mysql_query($query);
if (mysql_num_rows($result)!=0){
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
        if ($row['id']==$id){
            $base_price=$row['price'];
            $_SESSION['cart'][$cart_count]['baseprice']=$base_price;
            $_SESSION['cart'][$cart_count]['name']=$row['name'];
            $_SESSION['cart'][$cart_count]['image']=$row['image'];
            $_SESSION['cart'][$cart_count]['link']='/shop/'.$row['cat'].'/'.$row['subcat'].'?id='.$row['id'];
            if ($row['cat']=='the_uncut_sheet'){
                $uncut_sheet_spidey_sense='y';
            }
        }
    }
}
Post Reply