Jump to content


Photo

A quirky session problem


  • Please log in to reply
11 replies to this topic

#1 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 03 August 2006 - 09:42 PM

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:
<?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)
?>
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?
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#2 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 03 August 2006 - 09:58 PM

What happens when you run this script?
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#3 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 03 August 2006 - 10:38 PM

You might want to try UNSETting the session value you want to change before you change it
Legend has it that reading the manual never killed anyone.
My site

#4 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 03 August 2006 - 11:17 PM

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.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#5 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 03 August 2006 - 11:48 PM

Is this a typo?
$s['n'] = $s;

Did you mean "$s['n'] = $n;"? If not, maybe it's because it's 12.45am here and I've been up since 6am, but I'm having difficulty seeing what that would do...
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#6 Crimpage

Crimpage
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts
  • LocationBrisbane, Australia

Posted 04 August 2006 - 12:01 AM

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...
<?php
function movearray($given_array)
{
    foreach($_SESSION as $key => $value)
    {
       if (isarray($value))
       {
            movearray($value);
        } else
        {
        $s[$key] = $value;
        }
    }   
}
?>

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...


#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 04 August 2006 - 12:04 AM

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:
<?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
?>

Ken

#8 Crimpage

Crimpage
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts
  • LocationBrisbane, Australia

Posted 04 August 2006 - 12:07 AM

Yes, going on from what Ken just said. The code above may help you move the array data, but I dont know why you would.

#9 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 04 August 2006 - 12:11 AM

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.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#10 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 04 August 2006 - 12:21 AM

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:
<?php
		session_name('test');
		session_start();
		$n = $_SESSION['n'];
		echo $n;
		$n++;
		$_SESSION['n'] = $n;
		print_r($_SESSION);
// this successfull increments n across refreshes
?>
Long story short I just cycled through my container array during __deconstruct and set
foreach ($_storage as $_key => $_value)
{
$_SESSION[$_key] = $_value;
}
It isn't really too much worse than just saying $_SESSION = array;  Thanks for your help.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#11 Crimpage

Crimpage
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts
  • LocationBrisbane, Australia

Posted 04 August 2006 - 12:42 AM

So you have data sitting in $_storage that you want to set to the $_SESSION variable.

Can you print_R($_storage) and let me know how it looks.

Thanks,

Dave

#12 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 04 August 2006 - 01:05 AM

Crimpage, basically what's going on is the following:
$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;
}
So, if I print_r storage at any point it will reflect what $_SESSION has (or will have) in it.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users