PHP Cookie Comboes


I have been working a lot with PHP lately. Today I had an unmerciful time with cookies. I was almost done with a script when cookie trouble led me to go back and tear the whole script apart. It turns out that my problem lie in an imperfect understanding of how cookies work. First, you should know that they can not be created after any output. This is well known, however there is more to it. I made up the following test script to illustrate the possibilities.

//Just set a cookie.
setcookie(‘test1cook’, ‘testval1′, time()+3600);

//Set a cookie and set it again.
setcookie(‘test2cook’, ‘testval2′, time()+3600);
//setcookie(‘test2cook’, ‘testval2redux’ time()+3600); will throw a parse error.

//Set a cookie unset it, and reset.
setcookie(‘test3cook’, ‘testval3′, time()+3600);
setcookie(‘test3cook’, ‘testval3′, time()-3600);
setcookie(‘test3cook’, ‘testval3reset’, time()+3600);

//Set a cookie and use the unset function.
setcookie(‘test4cook’, ‘testval4′, time()+3600);
unset($_COOKIE['test4cook']);

//Set a cookie and attempt to change its contents.
setcookie(‘test5cook’, ‘testval5′, time()+3600);
$_COOKIE['test5cook'] = ‘tesval5_overwritten’;

//Print before setting a cookie.
print ” “;
setcookie(‘test6cook’, ‘testval6′, time()+3600);

Run this script and then run the following directly after:

print ‘<br />test1cook = ‘;
print $_COOKIE['test1cook'];
print ‘<br />test2cook = ‘;
print $_COOKIE['test2cook'];
print ‘<br />test3cook = ‘;
print $_COOKIE['test3cook'];
print ‘<br />test4cook = ‘;
print $_COOKIE['test4cook'];
print ‘<br />test5cook = ‘;
print $_COOKIE['test5cook'];
print ‘<br />test6cook = ‘;
print $_COOKIE['test6cook'];

The results will look something like this on your browser:

test1cook = testval1
test2cook = testval2
test3cook = testval3reset
test4cook = testval4
test5cook = testval5
test6cook =

As you can see in number 2, directly reseting the cookie does not change it in fact it throws an error. It must first be unset as in number three. Simply using the “unset()” function does nothing as illustrated in number 4. Rewriting the cookie variable also does nothing. And, of course, outputing anything before a cookie is made will result in no cookie.

Now the results here deserve another looksee. Because if a subsequent script runs some of these lines there will be some changed results:

//Set a cookie and set it again.
setcookie(‘test2cook’, ‘testval2′, time()+3600);

//Set a cookie and use the unset function.
unset($_COOKIE['test4cook']);

//Set a cookie and attempt to change its contents.
$_COOKIE['test5cook'] = ‘tesval5_overwritten’;

With the same print statements as above run concurrently, note these results:

test1cook = testval1
test2cook = testval2
test3cook = testval3reset
test4cook =
test5cook = tesval5_overwritten
test6cook =

Notice that now there is no error on number 2.  On number 4 the cookie is shown as unset, and number five is overwritten where it was not in the first go round, because the cookie is not available in the same run in which it is created.

Interestingly enough, another running of the print statement nets the following.

test1cook = testval1
test2cook = testval2
test3cook = testval3reset
test4cook = testval4
test5cook = testval5
test6cook =

This shows that the changes to the cookie array in 4 and 5 were only within the script. They did not effect the information stored on the browser, which can only be changed by first destroying the cookie and then recreating it.

  1. No comments yet.

You must be logged in to post a comment.