首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在php中设置cookie和重定向?

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

Stack Overflow用户
提问于 2016-08-13 10:24:05
回答 1查看 998关注 0票数 0

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

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

代码语言:javascript
复制
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)”

有什么办法解决这个问题并让它发挥作用吗?

谢谢你的帮忙!

+++更新+++

错误现在消失了。但是,当我访问页面"test-2“时,cookie并没有被存储。

下面是我正在使用的更新代码:

代码语言:javascript
复制
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;
}
}
EN

回答 1

Stack Overflow用户

发布于 2016-08-13 10:30:48

有很多错误。无论你在哪里放一个‘而不是’

代码语言:javascript
复制
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;
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38931732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档