Yarin
Yarin

Reputation: 183599

Why does my cookie value not persist across pages?

I have a WordPress site where we're tracking users with cookies unique to the session. I have the following code in my functions.php file:

if (!session_id())
    session_start();

// session user id:
if (isset($_COOKIE["my_user_id"])) {
    $my_user_id = $_COOKIE["my_user_id"];
}
else {
    $my_user_id = uniqid();
    setcookie("my_user_id", $my_user_id);
}

And on each page:

echo $_COOKIE["my_user_id"];

I would expect this to show the same user ID on each page when I was clicking around- however, the user ID is changing for different pages, though it remains the same throughout multiple clicks on the same page. Wouldn't a cookie value be site wide?

Upvotes: 2

Views: 3309

Answers (1)

Femi
Femi

Reputation: 64700

It is possible that the Wordpress cookie path isn't being set to the / value. See the arguments for setcookie.

Upvotes: 4

Related Questions