homepage Welcome to WebmasterWorld Guest from 54.204.168.212
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Cookies Array
d40sithui

5+ Year Member



 
Msg#: 3233083 posted 5:37 pm on Jan 26, 2007 (gmt 0)

Hi,
I'm just testing out the cookie function to set/retrieve a cookie array. Somehow the number of elements in the array will only go to 20 and no more. The script below will set a cookie array, and incrementing the position at every refresh. So at every refresh, it'll print array[1] = 1, array[2] = 2 ...and so on. Does anyone know why it stops at about 20 elements? Any input would be appreciated. thanks.

----------
<?

//retrieving and assigning cookie
$cart = $_COOKIE['cart'];

//if cookie is empty
if (empty($cart) !isset($cart)) { setcookie("cart[i]", 1); $cart = $_COOKIE["cart"]; }

$max = $cart[1];

//finds last position of cookie array
for ($i = 1; $i <= sizeof($cart); $i++)
{
if ($cart[$i] > $max) { $max = $cart[$i];}
}

//increment array position
$max++;

//attempts to set cookie (success)
if(setcookie("cart[$max]", $max)){
echo "Cookie set!<br>\n";
}
//failed to set cookie
else{ echo "Failed to set cookie <br>\n"; }

//prints all elements of cookie (array) cart
if(!empty($cart)){
for ($j = 1; $j <= sizeof($cart); $j++){
$temp = $cart[$j];
echo "\$cart[$j] : $temp <br>\n";
}
}

?>

--------

-khanh

 

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3233083 posted 1:11 am on Jan 27, 2007 (gmt 0)

you could try using serialize [php.net] on your array before you pop it into the cookie to see if that allows it to be larger

andreasfriedrich

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3233083 posted 8:28 am on Jan 28, 2007 (gmt 0)

You might be running into restrictions on the number of cookies a user agent will accept. I believe for IE the number is about 20. Firefox accepts about 50 cookies. It is generally better to store the cart content on the server.

jatar_k

WebmasterWorld Administrator jatar_k us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3233083 posted 1:43 pm on Jan 28, 2007 (gmt 0)

Welcome back andreasfriedrich

pageoneresults

WebmasterWorld Senior Member pageoneresults us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3233083 posted 7:08 pm on Jan 29, 2007 (gmt 0)

andreasfriedrich

Wow, haven't seen that username for a while. Yes, welcome back.

henry0

WebmasterWorld Senior Member henry0 us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3233083 posted 8:44 pm on Jan 29, 2007 (gmt 0)

I feel some new "Bag o'tricks" coming up!
Glad to see you around

andreasfriedrich

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3233083 posted 3:49 am on Jan 30, 2007 (gmt 0)

Thanks guys. These are quite some expectations you have there ;)

d40sithui

5+ Year Member



 
Msg#: 3233083 posted 6:15 pm on Jan 30, 2007 (gmt 0)

ahh ok that makes more sense. thanks

-khanh

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved