• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Problem with PHP cookies - some make it, some don't

I've got a form that I setcookie() on 29 fields.  When I complete the first few, they print out as expected on the final page, but when I complete all fields, the first few DON'T print out, just the remaining ones.  Any ideas?  I've been working on a solution on this one for many, many hours and just cannot come up with the solution!

btw, this only happens on IE6.  On Mozilla Firefox, it works perfectly.  It almost seems like IE6 will only let me have a certain # of cookies set.

Thanks for any help you can give...

code_red
0
code_red
Asked:
code_red
  • 11
  • 8
  • 2
4 Solutions
 
aksteveCommented:
Cookies have a max of 4k bytes to use. Are you sure you are not hitting this limit and maybe only IE6 is using the 4k rule?

Why not use sessions instead of passing all that info back and forth in a cookie?
0
 
code_redAuthor Commented:
I'm new to cookies, used quite a bit of php though.  Not familiar with sessions.   Can you give me more info on initializing a session on my form?  I've been working so long on cookies that I think I've fried my brain.  Thanks for the quick response!
0
 
aksteveCommented:
Place session_start(); at the VERY top of all pages that will use the session.

Then just assign variables to the session.

$_SESSION[variable1] = "value1";
$_SESSION[variable2] = "value2";

After you use the session_start() you can use all the $_SESSION variables you have stored.
To print out all the variables try doing a print_r($_SESSION);
0
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!

 
Diablo84Commented:
aksteve,

remember we need the single quotes for an errorless outcome: $_SESSION['vaiablename']

code_red,

For simplicity you might be able to make use of a session array here. This means that you can assign your form post data to the same variable (by name) rather then having to have different names for each.

eg.

$_SESSION['myarray'][] = $_POST['the_name_of_your_field'];
$_SESSION['myarray'][] = $_POST['the_name_of_another_field'];
$_SESSION['myarray'][] = $_POST['etc'];
$_SESSION['myarray'][] = "or assigning a string to the array";

By the end of it you then have all of your data stored in the array $_SESSION['myarray'].

You can then process the data, perhaps by looping through it like so:

foreach ($_SESSION['myarray'] as $var) {
 //reference $var for each individual item as it loops
}

or by using the index for referencing specific items, which by default begins at 0 and increments by 1 for each item in the array, eg:

echo $_SESSION['myarray'][0]; //outputs the value assigned to the first array item

Of course if the name of each variable is is important to relate to the original form fields name you can either do it the way aksteve showed using separate session variables or you can still use an array with associative indexes, eg:

$_SESSION['myarray']['index_name'] = $_POST['the_name_of_your_field'];
$_SESSION['myarray']['another_name'] = $_POST['the_name_of_another_field'];
//etc.

Then:

foreach ($_SESSION['myarray'] as $key => $var) {
 //reference $key for the array items key (eg. index_name) and $var for the value
}

Or, similar to before:

echo $_SESSION['myarray']['another_name']; //outputs the value associated with this array index

And as mentioned by aksteve you must always have session_start(); on each page using sessions BEFORE any output.

Diablo84
0
 
code_redAuthor Commented:
Thank you both, very much!  I will try this today and let you know if I run across any issues.  One question for now though... One of my forms has over 200 fields.  At the end of the form, I'd like to have the results displayed as $k:  $v.  Can I do this with sessions?  It seems the code at the end is pretty much like printing out cookies like this.   Here is my "cookie code" I'm using to print out only the $k: $v that has a value in it.  Could I do this with sessions too?

foreach ($_COOKIE as $k => $v) {
 print "$k: $v <br />";
}

This code works (only in Mozilla) to print out the cookies if they have a value entered into them.  Of course, I do want to use sessions now, so will it work basically the same?

Thanks again....

code_red
0
 
Diablo84Commented:
yes it's the same principle :)

foreach is used with arrays, each of the superglobals is effectively an array (COOKIE, POST, SESSION etc).

So you would use the associative array method i showed above and then output it using foreach, basic eg:

$_SESSION['myarray']['index1'] = "some value";
$_SESSION['myarray']['index2'] = "some other value";
$_SESSION['myarray']['index3'] = "another value";

Then later:

foreach ($_SESSION['myarray'] as $key => $var) {
 echo "$key : $var\n<br>";
}

Will output:

index1 : some value
index2 : some other value
index3 : another value
0
 
code_redAuthor Commented:
ok, as expected, i do have a question.  i used a mixture of both of your code suggestions, and it works!  thanks!!  one problem i'm having though is that every one of the session variables prints out as $k: $v.  what i want is ONLY the variables that were entered to print.  make sense?  i.e. if a user enters their name, but does not enter their email address.  i only want it to print
name: larry

not
name: larry
email:

i was having this problem with fwrite to a text file, but added a "!" to the code and it worked fine after that.

any ideas?  thanks again....

code_red
0
 
Diablo84Commented:
Conditional if statements can restrict output, for example:

foreach ($_SESSION['myarray'] as $key => $var) {
 if (!empty($var)) {
  echo "$key : $var\n<br>";
 }
}

With the above it will only output a value is not empty [http://us4.php.net/manual/en/function.empty.php].

The problem you mentioned with fwrite is likely to be down to an error in logic so adding the ! effectively reveresed the condition (ie. do this if that is not true RATHER THEN do this if that is true).
0
 
code_redAuthor Commented:
that was it!  thank you so much for your help!

code_red
0
 
Diablo84Commented:
no problem :)

Best Wishes

Diablo84
0
 
code_redAuthor Commented:
ok, i've run across another problem... in my form, i've got a series of checkboxes for options on a vehicle (31 to be exact).  this variable passes through 4 pages, and i'm using sessions.

page1:
<input name="Options" type="checkbox"  value="AC">
<input name="Options" type="checkbox" value="CD">
<input name="Options" type="checkbox" value="Convertible">

page2:
//passing the value through

<input type='hidden' name='Options' value='<?echo $Options?>'>

page3:
//the page that starts the session()

$_SESSION['Options'] = "$Options";

page4:
//where the results print out

<?
 foreach ($_SESSION as $k => $v) {
 if (!empty($v)) {
 echo "$k : $v\n<br>";
}
 }
?>

at this point, this prints out only the last option chosen.

now, i know i need to do something to this as an array, but i'm just not sure how to go about it.

hope you can help.

code_red
0
 
Diablo84Commented:
Two stages here, first of all you need to make your checkboxes a html array so you can have multiple selections, this is done by adding [] after the name, ie:

<input name="Options[]" type="checkbox"  value="AC">
<input name="Options[]" type="checkbox" value="CD">
<input name="Options[]" type="checkbox" value="Convertible">

Now, this calls for a small change in the way you output the code because when it comes to this we don't just have a key and a value we have another array of data. SO, something like this should work for you:

<?
foreach ($_SESSION as $k => $v) {
 if (is_array($k)) {
  foreach ($k as $value) echo "Options : $value\n<br>";
 }
 else {
  if (!empty($v)) echo "$k : $v\n<br>";
 }
}
?>

If there is any problems with the above or it needs tweaking at all post back :)
0
 
code_redAuthor Commented:
now it doesn't print out the options at all.  any idea?  thanks again for being so quick in responding!
0
 
Diablo84Commented:
sorry, slight error on my part:

 if (is_array($k)) {
  foreach ($k as $value) echo "Options : $value\n<br>";

should be:

 if (is_array($v)) {
  foreach ($v as $value) echo "Options : $value\n<br>";

in the above code.

It should work fine after that.
0
 
code_redAuthor Commented:
well, now it comes out as:

Options: Array

0
 
Diablo84Commented:
ok, i set up a test environment for the snippet of code above and it works as expected there so there might be a problem if your code prior to that.

Rather then carrying on down the educated guesses path it would be easier if i could see your code, so if you could post exactly what you have now...
0
 
code_redAuthor Commented:
sure, i think it has something to do with how it's passing through the pages, or the session.

page1:
<input name="Options[]" type="checkbox"  value="AC">
<input name="Options[]" type="checkbox" value="CD">
<input name="Options[]" type="checkbox" value="Convertible">

page2:
//passing the value through

<input type='hidden' name='Options' value='<?echo $Options?>'>

page3:
//the page that starts the session()

$_SESSION['Options'] = "$Options";

page4:
//where the results print out

<?
foreach ($_SESSION as $k => $v) {
if (is_array($v)) {
  foreach ($v as $value) echo "Options : $value\n<br>"; }
 else {
  if (!empty($v)) echo "$k : $v\n<br>";
 }
}
?>

how's that?  let me know if you need anything else.  thanks!!!
0
 
Diablo84Commented:
ok, i see whats going wrong.

Here:

<input type='hidden' name='Options' value='<?echo $Options?>'>

Where you echo options it is an array so all that is outputted is the string "Array" (you can't just output an array like this) so the reason you are seeing Array later on is simply because that is the string passed to the page, the array is lost before then.

I presume you have other things happening on each of these pages however i am slightly confused as to why you are passing the array via a hidden input on page 2, you can do it this way (with a little extra code) but it would be a lot easier to assign the array to a session variable on page 2 rather then waiting until page 3.

If there is a specific reason why you need to use a hidden field you will have to implode the array so it can be passed as a string and then explode it on page 3, it's not the most convienient option here so i will leave it at that right now unless you do wish to go with this method.

So, i would advise adding session_start(); at the top of your second page and add $_SESSION['Options'] = $_POST['Options'];

Then on page 3 if you have more session data to add just initialize the data as normal (with session_start();) and assign the data to the SESSION array.

If you have any more questions/problems relating to this please do feel free to ask.
0
 
code_redAuthor Commented:
ok, ok, i know i'm starting to annoy.  but, this is my final script:

foreach ($_SESSION as $k => $v) {
if (is_array($v)) {
  foreach ($v as $value) echo "$k: $value\n<br>"; }
 else {
  if (!empty($v)) echo "$k: $v\n<br>";
 }
}

and this is what's happening.... there are several fields on the form, where somewhere in the middle is the array i'm calling.  what's going on though is that the fields before the array are printing out, the array is printing out (thanks to you!), but nothing else prints out after the array.  make sense?

code_red
0
 
code_redAuthor Commented:
hold on, let me clarify some more issues.  as well as the sessions, so the customer can view his results at the end, the reason i'm passing the value using the hidden fields is because i'm also fwrite-ing the values to a txt file.  i had that script working fine, but with changing it to an array so the customer can view using sessions, that screwed the "options" field up so that it won't fwrite properly.  HELP!

code_red
0
 
code_redAuthor Commented:
I got it to work.  I am still having a problem with when my array fwrites, it has a strange string between each value.  ah well, i will soon find the answer to this one.  thanks for all your help.

code_red
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 11
  • 8
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now