Jump to content

A quirky session problem


Buyocat

Recommended Posts

Hi, just when I thought I had session down pat I run into some strange behavior; maybe you guys can help me.  Below is some test code that I think will make clear my problem:
[code]
<?php
session_name('test');
session_start();
$s = $_SESSION;
print_r($s); // will print out array()
$n = $s['n'];
$n++;
$s['n'] = $s;
$_SESSION = $s;
print_r($_SESSION); // will print out array(n => 1)
?>
[/code]
So, as may now be clear I can't take the session array set it to a second array, manipulate that second array and then try to set it back as the session data.  This is really a bummer because I wanted to grab the session data then clean and store it in a separate wrapped in an object, but if I can't store it in a separate array then it seems like I'm stuck interacting with the global array, which I would like to avoid.  Anyone know what's wrong with the above code or how to get around the issue?
Link to comment
https://forums.phpfreaks.com/topic/16495-a-quirky-session-problem/
Share on other sites

I have tried unsetting $_SESSION but that doesn't help.  Arthur, when I run the script it prints out what I put in comments, which is it prints an empty array and then prints out an array with n => 1.  What I expect it to do is increment n with each refresh, which it isn't doing.
I don't think you can just say
$s = $_SESSION;

I think you will need to iterate through each value in the array and move it to the new array. I don't have any code but it would be similar to this...
[code]
<?php
function movearray($given_array)
{
    foreach($_SESSION as $key => $value)
    {
      if (isarray($value))
      {
            movearray($value);
        } else
        {
        $s[$key] = $value;
        }
    } 
}
?>
[/code]

So that will go down as far as necessary through the arrays until it finds something that is not an array and moves it to $s.

I havent tested it, but something similar to this should work.

Cheers,

Dave

EDIT: Actually, looking over this, if there is sub array's it will not keep it. You should be able to modify it so that the if(isarray) should create the subarray, then add the values to that subarray. I hope that makes sense...
I don't think you fully understand how to use sessions. $_SESSION is an  array. When you want to be able to see the values of different valuables between different invocations of scripts, you store the values of the variables in an element of the $_SESSION array and you use an index to uniquely identify that element. These elements can contain simple values, arrays, or (I believe) objects. You put stuff into the array and pull stuff out, you don't replace the whole array with another one.

What you want to do would be something like this:
[code]<?php
session_start();
$n = (isset($_SESSION['n']))?$_SESSION['n']:0; // set n to 0 if it doesn't exist otherwise set it to the value stored in the session
echo $n."<br>\n";
$_SESSION['n'] = ++$n; // increment by one and store back in the session
?>[/code]

Ken
Maybe I don't understand, that's why I am posting here; that said it would surprise me greatly if you couldn't simply replace the session array with another array.  What about the original array was different from the new one?  I was under the impression what was significant was the title.  Either way there is a very good reason for wanting to transport session data out of the global variable.  If the only way to do so is to iterate through each value and then later iterate back and re-assign things then so be it, but that strikes me as awkard. 

To answer another person's question, yes that space was a typo.
Having tried what some of you suggested it does seem to be the case that the original $_SESSION value is important.  So while the above code does not work the following does:
[code]
<?php
session_name('test');
session_start();
$n = $_SESSION['n'];
echo $n;
$n++;
$_SESSION['n'] = $n;
print_r($_SESSION);
// this successfull increments n across refreshes
?>
[/code]
Long story short I just cycled through my container array during __deconstruct and set
[code]
foreach ($_storage as $_key => $_value)
{
$_SESSION[$_key] = $_value;
}
[/code]
It isn't really too much worse than just saying $_SESSION = array;  Thanks for your help.
Crimpage, basically what's going on is the following:
[code]
$this->_storage = array();
foreach ($_SESSION as $_key => $_value)
{
$this->_storage[$_key] = $_value;
}
// do some stuff here such as set new storage values or change/remove existing ones

// when the __destruct method is called for a certain class...
foreach ($this->_storage as $_key => $_value)
{
$_SESSION[$_key] = $_value;
}
[/code]
So, if I print_r storage at any point it will reflect what $_SESSION has (or will have) in it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.