Cookie only detected when visitor is logged in

admin2025-06-05  3

This is my first time playing around with cookies, so i apologize if the question is "stupid", but i did not manage to find an answer anywhere else on this site.

Iv'e set a cookie using this code in my child theme functions.php file:

<?php
$_fa = 'friend';

if(!isset($_COOKIE['myCookie'])) {

// set a cookie for 1 year
setcookie('myCookie', $_fa, time()+31556926, "/");

$_COOKIE['myCookie'] = $_fa;

}
}
?>

The cookie is set site wide, with the correct value, and I can access the cookie and its information in chrome by looking at the cookies manually.

The problem is:

If i am browsing the website while I am logged in, the functions that depend on the cookie, runs smoothly, as in, they manage to fetch the cookie name and value to perform different tasks.

But, when i log out and browse the website as a random visitor, none of the functions fire, becouse they do not detect the cookie name or value at all.

I also ran a test to confirm, with this code:

<?php
if(!isset($_COOKIE['myCookie'])) {
echo "Ooops! Cookie not found!";
} else {
echo 'Hello ' . htmlspecialchars($_COOKIE["myCookie"]) . '!';
}
?>

This returns:

Ooops! Cookie not found! When I am browsing as a random visitor "not logged in".

And

Hello friend! When I am browsing while logged in as a WP user.

This happens on every page on the website and in Chrome, Edge and Firefox (these are the browsers I've tested)

Just to clarify, the cookie is ALWAYS in the browser. It's not deleted when i log out or anything like that. It's just that none of the functions or statements detect the cookie once i browse the site, without being logged in.

What in my code is creating this behavior, and what can I do to fix this issue?

This is my first time playing around with cookies, so i apologize if the question is "stupid", but i did not manage to find an answer anywhere else on this site.

Iv'e set a cookie using this code in my child theme functions.php file:

<?php
$_fa = 'friend';

if(!isset($_COOKIE['myCookie'])) {

// set a cookie for 1 year
setcookie('myCookie', $_fa, time()+31556926, "/");

$_COOKIE['myCookie'] = $_fa;

}
}
?>

The cookie is set site wide, with the correct value, and I can access the cookie and its information in chrome by looking at the cookies manually.

The problem is:

If i am browsing the website while I am logged in, the functions that depend on the cookie, runs smoothly, as in, they manage to fetch the cookie name and value to perform different tasks.

But, when i log out and browse the website as a random visitor, none of the functions fire, becouse they do not detect the cookie name or value at all.

I also ran a test to confirm, with this code:

<?php
if(!isset($_COOKIE['myCookie'])) {
echo "Ooops! Cookie not found!";
} else {
echo 'Hello ' . htmlspecialchars($_COOKIE["myCookie"]) . '!';
}
?>

This returns:

Ooops! Cookie not found! When I am browsing as a random visitor "not logged in".

And

Hello friend! When I am browsing while logged in as a WP user.

This happens on every page on the website and in Chrome, Edge and Firefox (these are the browsers I've tested)

Just to clarify, the cookie is ALWAYS in the browser. It's not deleted when i log out or anything like that. It's just that none of the functions or statements detect the cookie once i browse the site, without being logged in.

What in my code is creating this behavior, and what can I do to fix this issue?

Share Improve this question edited Dec 27, 2018 at 22:55 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Dec 27, 2018 at 20:09 re-bootre-boot 479 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

ANSWER TO MY OWN QUESTION

This was something I had been trying to debug for the last couple of days. And the answer was found by mistake.

What caused the error was caching. I had to exempt the cookie from cache in order for it to work as it should.

So, if you are experiencing this behavior and start a debug quest that you know will bring you pain and misery, check your server settings or give your hosting provider a call first.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749063720a316042.html

最新回复(0)