?
Solved

setcookie array problem

Posted on 2005-04-13
10
Medium Priority
?
787 Views
Last Modified: 2008-03-10
hello i have a file to add_cook like this

setcookie("item_codes[$orders]",$_POST['code']);
$orders is a counter another cookie.

and a file to show cookies
for ($i=0;$i<=$orders-1;$i++){
      print $item_codes[$i];
}

now the problem is upto 19th offset it work fine on adding 20th item it start giving error on previos offsets undefined offset 0
again adding it give error to second offset like undefined offset 1 what can be the problem. please help me as soon as possible.
0
Comment
Question by:zahidrahim
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 7

Expert Comment

by:Promethyl
ID: 13771942

setcookie("item_codes".$orders,$_POST['code']);

$orders is a counter another cookie.

and a file to show cookies
for ($i=0;$i<=$orders-1;$i++){
      print $_COOKIE['item_codes'.$i];
}

Also, use print_r($_COOKIE); to debug.
0
 
LVL 1

Author Comment

by:zahidrahim
ID: 13772120
Now
Notice: Undefined index: item_codes0 in .....................

this error start coming when i try to enter 21st item in this array :(
0
 
LVL 7

Expert Comment

by:Promethyl
ID: 13772169
print_r($_COOKIE);

What does this say? Should we serialize the item_codes... make it an array?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:zahidrahim
ID: 13772375
it start removing previous offset entries from 0 and start adding new one.
i read from articles that we can only store 20 cookies only, can we increase this by using some method.
and i am making a shopping cart, should i use table to add orders what u suggest?

0
 
LVL 7

Expert Comment

by:Promethyl
ID: 13772415
Yes.

Store your information in an array called $item_codes_array.
      
setcookie('items',serialize($item_codes_array));

and a file to show cookies:
print_r(unserialize($_COOKIE[items]));

This will give you the entire array on the next page.
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13777419
if ( !isset($_COOKIE['item_codes']) ) { $_COOKIE['item_codes'] = array(); }
setcookie('item_codes['.count($_COOKIE['item_codes']).']',$_POST['code']);

foreach ( $_COOKIE['item_codes'] as $code ) {
  echo $code, '<br/>';
}
0
 
LVL 7

Accepted Solution

by:
Promethyl earned 100 total points
ID: 13778002
Value is a string, not mixed.

Description
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] )

Source: http://us3.php.net/manual/en/function.setcookie.php

This code won't work:

foreach ( $_COOKIE['item_codes'] as $code ) {
  echo $code, '<br/>';
}
0
 
LVL 11

Assisted Solution

by:ZhaawZ
ZhaawZ earned 100 total points
ID: 13779141
Promethyl, did you try it before posting?
Why do you think it won't work? It works! It always has worked! It works same way as sending values through a form.

<?php
if ( !isset($_COOKIE['item_codes']) ) {
  $_COOKIE['item_codes'] = array();
}
if ( isset($_POST['code']) ) {
  setcookie('item_codes['.count($_COOKIE['item_codes']).']', $_POST['code']);
  $_COOKIE['item_codes'][] = $_POST['code']; //this adds new value to 'current' $_COOKIE['item_codes'] array
}

foreach ( $_COOKIE['item_codes'] as $code ) {
  echo $code, '<br/>';
}
?>
<form method="post" action="xxx.php">
  <input name="code">
</form>



Same way to make an array with form:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <input name="arr[]"/><br/>
  <input name="arr[]"/><br/>
  <input name="arr[]"/><br/>
  <input name="arr[]"/><br/>
  <input name="arr[]"/><br/>
  <input type="submit"/>
</form>
<?php
  if ( isset($_POST['arr']) ) { print_r($_POST['arr']); }
?>
This will make array called 'arr', not a string value called 'arr[]'
0
 
LVL 14

Expert Comment

by:huji
ID: 15989394
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
Split: Promethyl & Zhaawz

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Huji
EE Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question