Big Tree
Big Tree

Reputation: 80

$_SESSION multidimensional array issues

Please i need help, this code below works fine on my localhost, php5.3+ but on the server its not working fine. 5.2

    $_SESSION['myinv_id'] = $setinvid;
    $_SESSION['prod_name'][$i] = $prod_name;    
    $_SESSION['qty'][$i] = $clean_qty;
    $_SESSION['price'][$i] = $clean_price;
    $_SESSION['total'][$i] = $clean_total;
    $_SESSION['desc'][$i] = $desc;

when i did a var_dump

The first time contents were added, i had these.

    array(5) { 
     ["prod_name"]=> array(1) {[0]=> string(4) "SDFD" }
     ["qty"]=> array(1) {[0]=> string(2) "43"} 
     ["price"]=> array(1) { [0]=> string(2) "43" } 
     ["total"]=> array(1) { [0]=> string(4) "1849" } 
     ["desc"]=> array(1) { [0]=> string(6) "dsfsdf" } } 

works fine....as the array is returned but in an attempt to added a second value...i get these error...saying Fatal Error [] not expected for strings..when i did a var_dump...i had these...in these case, ['prod_name'] & ['desc'] has inevitable changed from array to string and an array value cannot be pushed into the stake.

    array(5) { 
     **["prod_name"]=> &string(5) "dddsd"** 
      ["qty"]=> &array(2) { 
        [0]=> string(2) "43" 
       [2]=> string(2) "45" }
       ["price"]=> &array(2) { 
  [0]=> string(2) "43" 
     [2]=> string(1) "3" } 
     ["total"]=> &array(2) { 
[0]=> string(4) "1849" 
        [2]=> string(3) "135" } 
      **["desc"]=> &string(7) "dsddadd"** } 

Please what could be the source of the problem, and i have even tried decalaring this session variables as an empty array before using them, yet no avail.

Upvotes: 1

Views: 122

Answers (1)

goat
goat

Reputation: 31813

Your question seems to be why is my session array mysteriously being modified all by itself?

If the php config has register_globals enabled, then, $var and $_SESSION['var'] become the same variable(via references). This can lead to some real code wtf's because assigning a value to $var also assigns the value to $_SESSION['var']

I can't remember, but the reference between the two variable may only be established when session_start is called. Also, I would imagine this only occurs in the global scope.

Upvotes: 2

Related Questions