admin管理员组文章数量:1130349
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 badges1 Answer
Reset to default 1ANSWER 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.
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 badges1 Answer
Reset to default 1ANSWER 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.
本文标签: Cookie only detected when visitor is logged in
版权声明:本文标题:Cookie only detected when visitor is logged in 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749063695a2310486.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论