Alexander Farber
Alexander Farber

Reputation: 22958

PHP: setting cookies - the old values isn't replaced for some reason

I have a script where I set 2 cookies:

$month = time() + 60 * 60 * 24 * 30;
setcookie('id', $id, $month, '/');
setcookie('auth', $auth, $month, '/');
header('Content-Type: text/html; charset=utf-8');
print('<html><body>...etc....');

This does work well, but: some users have several id's, depending on from which page (social network) they access my script through an iframe.

A user having several id's is not a problem. But my problem is that when I ask that person to look at his cookies, he'll report that there are several cookies called id and auth. And I can reproduce it myself too.

And I was actually expecting there always to be just 1 id and 1 auth cookie.

What can I do here?

Doesn't calling setcookie('id', ...) with a new value replace the old value?

Thank you! Alex

Upvotes: 0

Views: 2210

Answers (1)

RobertPitt
RobertPitt

Reputation: 57258

Cookies are based on a KV Scheme (Key=Value) concept and the Key's act as unique identifiers.

The three primary effectors of setcookie are

  • CREATE
    • Create a cookie with setcookie("id","value")
  • READ
    • Read a cookie with $_COOKIE["id"]
  • UPDATE
    • Update a cookie with setcookie("id","new value")
  • DELETE
    • Delete by setting the expiration in the past setcookie ("TestCookie", "", time() - 10);

so yes your correct in your question, you should have a look at other factors that may deter the cookie states.

Upvotes: 1

Related Questions