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?
It is possible that the Wordpress cookie path isn't being set to the /
value. See the arguments for setcookie.
精彩评论