Buyocat Posted August 3, 2006 Share Posted August 3, 2006 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]<?phpsession_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 More sharing options...
king arthur Posted August 3, 2006 Share Posted August 3, 2006 What happens when you run this script? Link to comment https://forums.phpfreaks.com/topic/16495-a-quirky-session-problem/#findComment-68860 Share on other sites More sharing options...
AndyB Posted August 3, 2006 Share Posted August 3, 2006 You might want to try UNSETting the session value you want to change before you change it Link to comment https://forums.phpfreaks.com/topic/16495-a-quirky-session-problem/#findComment-68870 Share on other sites More sharing options...
Buyocat Posted August 3, 2006 Author Share Posted August 3, 2006 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. Link to comment https://forums.phpfreaks.com/topic/16495-a-quirky-session-problem/#findComment-68878 Share on other sites More sharing options...
king arthur Posted August 3, 2006 Share Posted August 3, 2006 Is this a typo?[code]$s['n'] = $s;[/code]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... Link to comment https://forums.phpfreaks.com/topic/16495-a-quirky-session-problem/#findComment-68882 Share on other sites More sharing options...
Crimpage Posted August 4, 2006 Share Posted August 4, 2006 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]<?phpfunction 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,DaveEDIT: 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... Link to comment https://forums.phpfreaks.com/topic/16495-a-quirky-session-problem/#findComment-68885 Share on other sites More sharing options...
kenrbnsn Posted August 4, 2006 Share Posted August 4, 2006 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]<?phpsession_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 sessionecho $n."<br>\n";$_SESSION['n'] = ++$n; // increment by one and store back in the session?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/16495-a-quirky-session-problem/#findComment-68887 Share on other sites More sharing options...
Crimpage Posted August 4, 2006 Share Posted August 4, 2006 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. Link to comment https://forums.phpfreaks.com/topic/16495-a-quirky-session-problem/#findComment-68890 Share on other sites More sharing options...
Buyocat Posted August 4, 2006 Author Share Posted August 4, 2006 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. Link to comment https://forums.phpfreaks.com/topic/16495-a-quirky-session-problem/#findComment-68891 Share on other sites More sharing options...
Buyocat Posted August 4, 2006 Author Share Posted August 4, 2006 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. Link to comment https://forums.phpfreaks.com/topic/16495-a-quirky-session-problem/#findComment-68893 Share on other sites More sharing options...
Crimpage Posted August 4, 2006 Share Posted August 4, 2006 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 Link to comment https://forums.phpfreaks.com/topic/16495-a-quirky-session-problem/#findComment-68900 Share on other sites More sharing options...
Buyocat Posted August 4, 2006 Author Share Posted August 4, 2006 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. Link to comment https://forums.phpfreaks.com/topic/16495-a-quirky-session-problem/#findComment-68912 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.