Link to home
Start Free TrialLog in
Avatar of code_red
code_redFlag for Afghanistan

asked on

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
SOLUTION
Avatar of aksteve
aksteve

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of code_red

ASKER

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!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of Diablo84
Diablo84

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
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that was it!  thank you so much for your help!

code_red
no problem :)

Best Wishes

Diablo84
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
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 :)
now it doesn't print out the options at all.  any idea?  thanks again for being so quick in responding!
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.
well, now it comes out as:

Options: Array

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...
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!!!
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.
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
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
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