Jump to content

Problem reading a cookie


BillyB

Recommended Posts

Hello all. Newbie here. I have a question regarding cookies.

 

I have set two cookies in script #1, namely EditingTraining and EditSPMarketing, but only seem to be able to read one of them in script #2.

 

Excerpts from the script are below. I'm hoping the problem has something to do with the way that I've coded the scripts, but am really quite perplexed as to why this isn't working.

 

 

Script #1

setcookie("EditTraining", $row['EditTraining'], time()+5184000, '/');

setcookie("EditSPMarketing", $row['EditSPMarketing'], time()+5184000, '/');

 

Script #2

$EditTraining = $_COOKIE['EditTraining'];

$EditSPMarketing         = $_COOKIE['EditSPMarketing'];

echo "EditSPMarketing: $EditSPMarketing<br>";

echo "EditTraining: $EditTraining<br>";

 

Output

EditSPMarketing:

EditTraining: 1

 

As you can see from the output, EditTraining displays '1' while EditSPMarketing seems to contain a null value. I have used echo to display the value of both columns before I set the cookie and EditSPMarketing definitely displays a value, but only one seems to get written to a cookie.

 

What am I doing wrong here?

 

Thank you,

 

BillyB

 

Link to comment
https://forums.phpfreaks.com/topic/172797-problem-reading-a-cookie/
Share on other sites

I've finally found the answer. There is a LIMIT to the number of cookies that PHP can handle. That limit is 20! By using Garethp's print_r($_COOKIE); command I had, indeed, exceeded the Cookie Threshold. I did a bit more searching on the Interweb and came across this possible solution, which I will be trying out tomorrow.

 

PHP Cookie limit

Wondering why some cookies are not set? The maximum number is 20. The maximum total size only 2 KB. They are set in the reverse direction of the script. And they must be accessed by the same directory in which they have been set. If you use 'include' the original script will determine the active directory, not the included one.

You can exceed the number of 20 by putting the values into one string seperating with a character of you choice:

$all="$a;$b;$c;$d";

setcookie('all',$all,time()+10000);

 

 

and unpack it with explode:

$x=$_COOKIE['all'];

$y=explode(';',$x);  // ';' is the seperator

$a=$y[0];$b=$y[1]; and so on.....

 

Thank you for your help.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.