Jump to content
RyanJ

Php Session Help

Recommended Posts

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

 

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.

 

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.

 

The only place in the code that's being touched where the $_SESSION variable is being updated:

 

$cart = array();

if(isset($_SESSION["cart"]))

$cart = $_SESSION["cart"];

 

array_push($cart, $item);

 

$_SESSION["cart"] = $cart;

 

The site can currently be found at www.irish.josbweb.com.

 

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!

Share this post


Link to post
Share on other sites

Welcome to the forums Ryan :thumbdown:

 

Sorry I can't help with your issue but I am sure someone will be along shortly that will have an answer for you.

Share this post


Link to post
Share on other sites

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.

 

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.

Share this post


Link to post
Share on other sites

With a bit more echo-work, the session variable is blasted when the variable $cart is set to an array.

 

echo $_SESSION["cart"][0][1]; before the assignment returns the correct value.

echo $_SESSION["cart"][0][1]; after the assignment returns nothing.

 

The rest of the script is working as it should.

 

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.

 

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.

Edited by RyanJ

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

 

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.

Share this post


Link to post
Share on other sites

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 http://www.php.net/register_globals.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...