2016-11-23 59 views
0

我有一個頁面有兩個鏈接。當你點擊一個鏈接而不是PHP時,應該將你重定向到被點擊的頁面,當你回訪時,cookie應該記住你以前的選擇,它應該加載特定的頁面,而不是帶有兩個鏈接的頁面。 我能夠設置cookie,但我不知道如何使用它,當我點擊鏈接。鏈接點擊重定向後cookie設置爲PHP

下面是完整的代碼:

<?php 
    $cookie_name = 'redirect'; 
    if(!isset($_COOKIE[$cookie_name])) { 
     setcookie($cookie_name , 'redirect-link', time()+(60*60*24*365), "/"); 
    } 

    else { 
     print 'Cookie with name ' . $cookie_name. ' value is: ' . $_COOKIE[$cookie_name]; 
    } 
?> 

<html> 
 
<head></head> 
 
<body> 
 
<br/> 
 
<a href="https://link1.com">link1</a> 
 
<a href="https://link2.com">link2</a> 
 
</body> 
 
</html>

回答

0

主頁

if(isset($_COOKIE['thechoosenone']) 
{ 
    $url='<baseusl>/'.($_COOKIE['thechoosenone']; 
    header('Location: '.$url); 
} 

上的鏈接頁面如:

if(!isset($_COOKIE['thechoosenone'])) { 
//create cockie $_COOKIE['thechoosenone'] and asight the url 
}else{ 
    $url='<baseusl>/'.($_COOKIE['thechoosenone']; 
    header('Location: '.$url); 
    //stop choosing via manual url browsing 
} 
+0

您的回答很有用,但我不明白我如何在cookie中存儲選擇的點擊。我需要在主頁上提供代碼。 –

+0

您將需要使用maltiple頁反正 只有3種方式 1)是AJAX,並具有了新的一頁 2上的Cookie genaration)是具有鏈接頁面 3碼)創建JS餅乾 – Buddhi741