2016-08-13 100 views
0

我想设置一个cookie,如果用户访问页面“测试-2”。当cookie被设置并且访问者在24小时内尝试访问页面“测试”时,他应该被自动重定向到“测试-2”页面。如何在php中设置cookie和重定向?

下面是我插入我的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 +++

的错误,现在没有了。但是,cookie不会存储,当我访问网页“测试2”

这里是更新的代码我使用:

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; 
} 
} 

回答

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; 
} 
} 
+0

谢谢错误消失了!然而,当我访问页面“test-2”时没有设置cookie ... –

+0

@LukasLang,我更新了我的ans.plz检查 – coder

+0

感谢您的帮助!然而,cookie仍然没有设置... –