2016-08-13 29 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仍然沒有設置... –

相關問題