如何在wordpress中从头到模板页面获取cookie值?

时间:2011-05-31 06:14:00

标签: php wordpress cookies

我在header.php中设置了cookie,如下所示

<?php if($_GET['signup'] == '123' && !isset($_COOKIE['sign'])){
    setcookie("sign", "1", time()+3600);
} ?>

现在我需要在页面signup.php中获取此cookie值。但我无法重温该页面中的价值。在Signup.php页面中,我使用以下代码检查cookie,

<?php
/**
Template Name:  Sign Up
 */
$ck = $_COOKIE['sign'];
echo "cookie".$ck;
if(isset($_COOKIE['sign'])){
    header("Location: https://www.ap.com/app/signup.jsp?signup=123");
}
else
{
    header("Location: https://www.ap.com/app/signup.jsp");
}
?>

但它只进入else条件(如果也设置了cookie)。善意的建议......

1 个答案:

答案 0 :(得分:0)

尝试使用init操作创建插件和挂钩:http://codex.wordpress.org/Plugin_API/Action_Reference/init

function my_set_cookie() {
    if($_GET['signup'] == '123' && !isset($_COOKIE['sign'])) {
        setcookie("sign", "1", time()+3600);
    }
}

add_action('init', 'my_set_cookie');