$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Cookie only detected when visitor is logged in|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Cookie only detected when visitor is logged in

matteradmin11PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far