Why can't I change values in my cookies? I'm storing my users city in a cookie.

I want that cookie to expire by browser session, so I don't want to store this information in $_SESSION, because I want $_SESSION to persist for the default 23ish days.

So I'd like to stick with Drupal storing all of its stuff in $_SESSION as normal, but also set a cookie with a custom lifetime. When the user selects a city I set a cookie in my forms _form_submit() hook:

$c = 'Washington'; // ...or whatever
$_COOKIE['city']['cityname'] = $c;
setcookie('city[cityname]', $_COOKIE['city']['cityname'], 0);


If I print this out with print_r($_COOKIE['city']) I get:

Array ( [cityname] =&gt; Washington [state] =&gt; 65 )


Yay, it works!

Then the user selects a new city and the same code runs again (I know it's running because I've tried inserting a die; in there):

$c = 'New York';
$_COOKIE['city']['cityname'] = $c;
setcookie('city[cityname]', $_COOKIE['city']['cityname'], 0);


I print the $COOKIE value out again and get:

Array ( [cityname] =&gt; Washington [state] =&gt; 65 )


So the value hasn't changed and I'm left with the previous city. Why aren't my cookies values changing? Is there a way I can fix this, or a recommended workaround available?
OK, found the issue I think. The first cookie is set from the homepage. The second was set from a different page.

I looked in the Chrome "cookies" dialog and found out that I had multiple cookies all set with different paths.

setcookie() has a "$path" argument:


  The path on the server in which the cookie will be available on. If set to '/', the cookie will 
  be available within the entire domain. If set to '/foo/', the cookie will only be available
  within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default
  value is the current directory that the cookie is being set in.


So in my setcookie() I now do:

setcookie('region[state]', $state, 0, '/');


...and the path gets saved to "/" every time, and I don't end up with duplicate cookies causing confusion.