最新消息: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)

hooks - Cookie cant be read even cookie is present

matteradmin12PV0评论
add_action( 'wp', 'setCookies', 10); 

I am using this hook to set cookies. I am using 'wp' because I need to get the current post ID.

Now on my sidebar.php, I am calling if(isset($_COOKIE['cookiename'])) but it returns nothing.

I checked on chrome devtools and cookiename = postID is exisiting there.

When I refresh the page or go to another page, the cookie is now returning something.

So I'm guessing maybe my cookie is being set only after the sidebar.php is loaded? If so, is there a way around this?

add_action( 'wp', 'setCookies', 10); 

I am using this hook to set cookies. I am using 'wp' because I need to get the current post ID.

Now on my sidebar.php, I am calling if(isset($_COOKIE['cookiename'])) but it returns nothing.

I checked on chrome devtools and cookiename = postID is exisiting there.

When I refresh the page or go to another page, the cookie is now returning something.

So I'm guessing maybe my cookie is being set only after the sidebar.php is loaded? If so, is there a way around this?

Share Improve this question asked Mar 8, 2019 at 5:50 kydroninkydronin 212 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The $_COOKIE array contains only these cookies that came in the request from the browser.

setcookie() adds an HTTP header asking the client to send the cookie during subsequent requests.

So yeah - you can’t access $_COOKIE in the same request it is set.

Post a comment

comment list (0)

  1. No comments so far