heavyEddie Posted November 12, 2006 Share Posted November 12, 2006 I'm trying to work with cookies for the first time. It seems like it should be simple enough, but alas, I'm having some problems.[code] for($i = 0; $i < count($_POST['pid']); $i++) { $cookie_name = 'cart_item[' . $_POST['pid'][$i] . ']'; setcookie($cookie_name, $_POST['qty'][$i], 0, '/'); }[/code]I'm trying use cookies to store an array. The code seems to work as long as I'm setting all the cookies on the page at one time. For instance...[list][*]If I set a cookie called cart_item[22] = 2 it works fine.[*]If I set cookies called cart_item[22] =2 and another called cart_item[33] = 3 they are both stored fine.[*]However, if cart_item[22] is already set and the script is run to set cart_item[33]... it appears cart_item[22] is overwritten with cart_item[33].[/list] Link to comment https://forums.phpfreaks.com/topic/27044-array-in-a-cookie/ Share on other sites More sharing options...
trq Posted November 12, 2006 Share Posted November 12, 2006 You might want to look into using [url=http://php.net/serialize]serialize[/url] / [url=http://php.net/unserialize]unserialize[/url]. Link to comment https://forums.phpfreaks.com/topic/27044-array-in-a-cookie/#findComment-123666 Share on other sites More sharing options...
printf Posted November 13, 2006 Share Posted November 13, 2006 You can store it in a single value...// set it[code]$items = array ();for ( $i = 0; $i < sizeof ( $_POST['pid'] ); $i++ ){ $items[$_POST['pid'][$i]] = $_POST['qty'][$i];}if ( ! empty ( $items ) ){ setcookie ( 'store_cart' urlencode ( serialize ( $items ) ), 0, '/', '.domain.com' );}[/code]// extract it...[code]$cart = array ();if ( isset ( $_COOKIE['store_cart'] ) ){ $cart = unserialize ( urldecode ( $_COOKIE['store_cart'] ) );}[/code] Link to comment https://forums.phpfreaks.com/topic/27044-array-in-a-cookie/#findComment-123669 Share on other sites More sharing options...
heavyEddie Posted November 13, 2006 Author Share Posted November 13, 2006 Thanks guys... I will give it a run for it's money tomorrow. Link to comment https://forums.phpfreaks.com/topic/27044-array-in-a-cookie/#findComment-123720 Share on other sites More sharing options...
heavyEddie Posted November 14, 2006 Author Share Posted November 14, 2006 [url=http://www.php.net/manual/en/function.setcookie.php]This page[/url] on php.net states that using searilize with cookies is unsecure. Why would this be? Link to comment https://forums.phpfreaks.com/topic/27044-array-in-a-cookie/#findComment-124220 Share on other sites More sharing options...
Zane Posted November 14, 2006 Share Posted November 14, 2006 because cookies are stored on your computerand if you're able to use serialize and unserialize to read itso can anyone else...just by opening the cookieor at least that's my idea of it.it could be more than that Link to comment https://forums.phpfreaks.com/topic/27044-array-in-a-cookie/#findComment-124224 Share on other sites More sharing options...
heavyEddie Posted November 22, 2006 Author Share Posted November 22, 2006 In my case this was a non issue anyway since It isn't anything sensitive in nature. I won't be putting in SSN in a cookie that is for sure :) Link to comment https://forums.phpfreaks.com/topic/27044-array-in-a-cookie/#findComment-128248 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.