Jump to content

[SOLVED] Strange Session Error


Ninjakreborn

Recommended Posts

I am having this strange error with sessions and google checkout..

I am basically writing some of the code custom. I want to get a session of items and build the google checkout button but my sessions are behaving strangely...first time this has happened actually in the 6 years I have been working with PHP.  I have checked all the necessary Session info.

[phpinfo]
session

Session Support enabled
Registered save handlers files user sqlite
Registered serializer handlers php php_binary wddx

Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 On On
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_httponly Off Off
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 100 100
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.hash_bits_per_character 4 4
session.hash_function 0 0
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /tmp /tmp
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid 0 0
[/phpinfo]
Those are the settings. THe code I am using looks like:
<?php
/* Save item in a session for later */
if (!empty($_SESSION['items'])) {
  $count = count($_SESSION['items'])+1;
	  $_SESSION['items'][$count]['product_name']         = $_POST['product_name'];
	  $_SESSION['items'][$count]['product_description']  = $_POST['product_description'];
	  $_SESSION['items'][$count]['product_price']        = $_POST['product_price'];
  }else {
	  $_SESSION['items'][1]['product_name']         = $_POST['product_name'];
	  $_SESSION['items'][1]['product_description']  = $_POST['product_description'];
	  $_SESSION['items'][1]['product_price']        = $_POST['product_price'];  
  }

  $itemsarray = $_SESSION['items'];
  /* Build Shopping Cart Button */
$merchant_id = '###########';
$merchant_key = '##############';
$server_type = "production";
$currency = "USD";
$cart = new GoogleCart($merchant_id, $merchant_key, $server_type,
$currency);
  foreach($itemsarray as $items) {
    $googleitems = new GoogleItem($items['product_name'], $items['product_description'], 1, $items['product_price']);
    $cart->AddItem($googleitems);
    unset($googleitems);
  }
?>

This is the area where I am setting the data into a session array then using that array to build the google checkout button.  (Further down but not shown is the google checkout button because it's unrelated to this problem).

 

Now..if I add "ONE" item.  Then click the button it works. Sessions work, the google item creation works, it creates the button and let's me go to checkout...very simple. If I try to add more than 1 item...it's acting strange.  If I try to add "two" items it also works fine. Does sessions right, let's me add items, let's me go to the google checkout and it works great (even passes the items to google checkout via the button.

 

Now if I add in a third item. It messes up.  So far it seems it's messing up with the sessions themselves. It ends up not counting them properly, or not assigning them properly. Is there anything obviously wrong with the above code?

Link to comment
Share on other sites

It has something to do with the sessions themselves...

I tried a different approach and it seemed to work more fluidly at first..but just like before it screws up my session array on the 3rd item entry. It still handles 2 perfectly but messes up on the 3rd and above item additions.

<?php
if (!empty($_SESSION['items'])) {
    $item['product_name'] = $_POST['product_name'];
    $item['product_description'] = $_POST['product_description'];
    $item['product_price'] = $_POST['product_price'];
    $_SESSION['items'][] = $item;
	  //$_SESSION['items'][$count]['product_name']         = $_POST['product_name'];
	  //$_SESSION['items'][$count]['product_description']  = $_POST['product_description'];
	  //$_SESSION['items'][$count]['product_price']        = $_POST['product_price'];
  }else {
	  $_SESSION['items'][0]['product_name']         = $_POST['product_name'];
	  $_SESSION['items'][0]['product_description']  = $_POST['product_description'];
	  $_SESSION['items'][0]['product_price']        = $_POST['product_price'];  
  }
  echo '<pre>';
  print_r($_SESSION);
  echo '</pre>';
?>

This works great for the first and second items but when the third is added it screws it up and tries to put sub-key four inside the master array and destroys a giant chunk of my array and re-arranges everything around (basically just stops working).

 

Any advice?

Link to comment
Share on other sites

I found out....why it wasn't working.  It is strange. I guess I will never know "why" but when I was setting the session before doing all of my work with the google shopping cart it was screwing up my session..probably something the google system does internally. What I did was try a test case:

<?php
// Get my currently added item
  $newitem['product_name'] = $_POST['product_name'];
  $newitem['product_description'] = $_POST['product_description'];
  $newitem['product_price'] = $_POST['product_price'];
// Get existing item list from session
  $itemsarray['items']   = $_SESSION['items'];
// Set the new item into the array of items 
$itemsarray['items'][] = $newitem;

Then I do all of my stuff with google (get the items together, loop through item array and bind them to the

google button, create the google button. After ALL of that is done I come back and refinish my session doing.

<?php
  $_SESSION['items'] = $itemsarray['items'];
?>

That overwrites the current session with all of the new item data. This works perfectly throughout however many items I want to add.

 

I am marking this as solved since it's solved but if anyone later knows why this happened I wouldn't mind knowing just for educational purposes later. This was a strange workaround I had to perform for something that shouldn't have been happening in the first place.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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

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

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.