如何在PHP中设置cookie和重定向?

时间:2016-08-13 10:24:05

标签: php wordpress redirect cookies

如果用户访问Page" Test-2",我想设置一个Cookie。当设置cookie并且访问者试图访问Page"测试"在24小时内,他应该被自动重定向到Page" Test-2"。

这是我插入到Wordpress主题的function.php文件中的代码:

if( is_page('test-2’)){
    if(isset($_COOKIE['dz_offer'])){
        $cookie = $_COOKIE['dz_offer'];
    }
else{
    setcookie('dz_offer', time() + 60*60*24, '/');
    }
}

if( is_page('test‘)){   
    if (isset($_COOKIE['dz_offer‘])){
        header(„Location: https://esample.com/test-2“);
        exit;
    }
}

但是现在我发现以下错误:"解析错误:语法错误,意外' dz_offer' (T_STRING)"

任何想法如何解决这个问题并让它运作起来?

感谢您的帮助!

+++ UPDATE +++

错误现在消失了。但是,当我访问页面" test-2"

时,cookie不会被存储

以下是更新后的代码I使用:

if( is_page('test-2')){
if(isset($_COOKIE['dz_offer'])){
    $cookie = $_COOKIE['dz_offer'];
}
else{
setcookie('dz_offer',$val, time() + 60*60*24, '/');
}
}

if( is_page('test')){   
if (isset($_COOKIE['dz_offer'])){
    header("Location: https://example.com/test-2");
    exit;
}
}

1 个答案:

答案 0 :(得分:0)

有很多错误。你到处都放了一个`而不是'

if( is_page('test-2')){
if(isset($_COOKIE['dz_offer'])){
    $cookie = $_COOKIE['dz_offer'];
}
else{
//cookies should be changed in the following way and $val is the value you have to save in the cookie 'dz_offer'.
setcookie('dz_offer',$val, time() + 60*60*24);
}
}

if( is_page('test')){   
if (isset($_COOKIE['dz_offer'])){
    header("Location: https://esample.com/test-2");
    exit;
}
}