2011-04-13 20 views
0

我試圖在Wordpress操作中訪問cookie。動作註冊在下面,它適用於靜態數據。但是當我嘗試訪問一個cookie時,我知道一個cookie是有效的,因爲我在頁面上回顯它,總是空的。此操作是否可以訪問$ _COOKIE對象?如果不是我能做什麼解決方案?Cookie在Wordpress中爲空用PHP添加操作

add_action ('comment_post', array(&$cisco_rewards, 'add_comment_badge_id'), 1); 

function add_comment_badge_id($comment_id) { 

     add_comment_meta($comment_id, 'badge_id', $_COOKIE[Cisco_Rewards::REWARDS_COOKIE_NAME], true); // I put just a 2 here and it worked correctly, blank otherwise with the cookie. 
    } 

回答

1

$_COOKIEsuperglobal variable所以它是可用的任何對象(或任何範圍)內,以使得不能成爲問題。幾點思考:

您是否在打印$_COOKIE之前將其傳遞給add_comment_meta()以便您可以檢查密鑰和值?像:

function add_comment_badge_id($comment_id) 
{ 
    exit(print_r($_COOKIE)); 
    add_comment_meta($comment_id, 'badge_id', $_COOKIE[Cisco_Rewards::REWARDS_COOKIE_NAME], true); 
} 

檢查你setting the cookie與你正在做的測試相同的域名。也許你正在設置它與www.domain.com,您正在訪問的頁面與http://domain.com

還要注意,您在請求中設置了Cookie,並且它在下一個(SO中的see this brief but well explained answer)中可用。

+0

是的,餅乾甚至沒有在那裏。不知道EXIT在PHP(.NET開發人員在這裏)。主要問題不是在Cookie上設置path = /,而是在javascript中結束。 – 2011-04-13 20:48:26