Ninjakreborn Posted July 29, 2009 Share Posted July 29, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/167985-solved-strange-session-error/ Share on other sites More sharing options...
Ninjakreborn Posted July 29, 2009 Author Share Posted July 29, 2009 Also I have all of the basics in place (session_start()) above all pages and all of that good stuff as well. Quote Link to comment https://forums.phpfreaks.com/topic/167985-solved-strange-session-error/#findComment-886020 Share on other sites More sharing options...
Ninjakreborn Posted July 29, 2009 Author Share Posted July 29, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/167985-solved-strange-session-error/#findComment-886045 Share on other sites More sharing options...
Ninjakreborn Posted July 29, 2009 Author Share Posted July 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167985-solved-strange-session-error/#findComment-886076 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.