Php Session Help

liunx

Guest
Hey guys, I seem to be having some problems getting PHP sessions to work out on my site. I'm not the best scripter, but I can usually do well enough to get by. This time, though, I don't have as much time to try to track down what's messing up, so I was hoping for some help :).<br /><br />Basically, I have a site which works fine on my own server, but it is not working when moved to my TCH account. I've created a small shopping cart, but on TCH, the session only seems to remember the last item put into it.<br /><br />The session variable is $_SESSION["cart"], which holds an array of items, each item being an array of attributes as well. I plan on doing pulls from the database for the attributes in the future, but for now I'm just trying to get what I have now working.<br /><br />The only place in the code that's being touched where the $_SESSION variable is being updated:<br /><br />$cart = array();<br />if(isset($_SESSION["cart"]))<br /> $cart = $_SESSION["cart"];<br /><br />array_push($cart, $item);<br /><br />$_SESSION["cart"] = $cart;<br /><br />The site can currently be found at <!-- w --><a class="postlink" href="http://www.irish.josbweb.com">www.irish.josbweb.com</a><!-- w -->. /><br />Anyway, thanks for any help you can give. And if I've read other posts well enough over the years, I give a hardy "good to be here" in response to the incoming deluge of "welcome"s!<!--content-->
Welcome to the forums Ryan <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><br /><br />Sorry I can't help with your issue but I am sure someone will be along shortly that will have an answer for you.<!--content-->
Welcome to the forum, Ryan. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
With more echo work, here's the scenario: 1) Item is added to cart. All is well. 2) Another item is requested to be added to the cart. Immediately before the snippet above, the session variable is correct. Immediately after the snippet, the first item has been lost. The count of the array is only 1. 3) Repeat 2.<br /><br />Perhaps the session array can only accept simple scalar values? That doesn't seem correct since the site works on my machine. There was another difference between my machine and TCH that might be enlightening. Prior to uploading, the site worked fine. On uploading, I received errors because I was starting a session after generating output. This was corrected easily enough with some reorganization, but perhaps it highlights some difference that might lead to a solution.<!--content-->
With a bit more echo-work, the session variable is blasted when the variable $cart is set to an array.<br /><br />echo $_SESSION["cart"][0][1]; before the assignment returns the correct value.<br />echo $_SESSION["cart"][0][1]; after the assignment returns nothing.<br /><br />The rest of the script is working as it should.<br /><br />Edit: And I've got it working. Changing the variable name from $cart to $test makes it work, defying all reason that I can see (but still making me very happy!). $cart is not used prior in the script--its whole life can be seen in the snippet above.<br /><br />Anyone have any clue as to why the variable name would be messing with things on the TCH servers? I'm just ignorant and curious.<!--content-->
Are you modifying a package or writing your own? I can't see where $cart would be a reserved variable but possible. Is $test defined elsewhere and is it a global variable? Maybe if $cart were defined global it would also work.<!--content-->
Sorry for the delay--was out of reach of the computer for the weekend. I am not using an existing package at all. The whole of the script is 41 lines long, including line breaks, so there isn't much room for anything to be going wrong. $test is not defined anywhere else in the script, nor was $cart. Given how short the script is, the most scoping that occurs is in basic if blocks, and the variable is defined outside of one, in the highest block.<br /><br />The only thing unique to the word "cart" is that it's also the key to the cart within $_SESSION. Perhaps there is some shortcut enabled somewhere that allows you quick access to associative arrays by just using a variable with the same name as a key? That would explain the behavior well enough (setting $cart to a blank array would be blasting the session variable right before it's assigned). I'm going to test this idea out with another session variable in the near future when I get a little extra time.<br /><!--content-->
Sounds like you possibly have register_globals on? I believe it would cause the behavior you're seeing. If so, it is generally recommended that you turn it off by adding "php_flag register_globals off" to .htaccess as it's responsible for a lot of vulnerabilities in php scripts. Check out <a href="http://www.php.net/register_globals" target="_blank">http://www.php.net/register_globals</a>.<!--content-->
 
Top