PHP因为cookie无法正常工作

I am facing some trouble with a conditional statement that uses cookies in PHP.

I would like to change the state of an image based on whether a cookie is set or not. Here is my code (in file MAIN.PHP):

$cookie = "d_content_vote_".$databaseArray['id']; 
if(!isset($_COOKIE[$cookie])) {
  // display image 1 if cookie is not set
}
else { 
  // display image 2 if cookie is set
}

The cookie value (of the timestamp) is set in ../INCLUDES/RATING.PHP, and I make an ajax call to that file. In order to debug, I did a print_r($_COOKIE) in RATING.PHP which gave me this:

Array
(
    [d_content_vote_1] => 1402726678
    [d_content_vote_4] => 1402727148
    [PHPSESSID] => effa8778efbe1b3dfb5bb301e359997d
)

However, when I do a print_r($_COOKIE) in MAIN.PHP I do not get the d_content_vote_* cookies, only the session info.

How do I transfer the cookie that is set in rating.php so that I can use it in main.php. I have never faced this problem before.

[Additional info: I'm building the site on a MAMP server now]

查看全部
duanbi3151
duanbi3151
2014/06/14 06:34
  • php
  • cookies
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复